Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa-almaliki committed Sep 16, 2018
0 parents commit 81d530d
Show file tree
Hide file tree
Showing 36 changed files with 1,567 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Cron/FrontNameGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php declare(strict_types=1);

namespace Alaa\DynamicFrontName\Cron;

use Alaa\DynamicFrontName\Exception\FrontNameCreateException;
use Alaa\DynamicFrontName\Logger\Logger;
use Alaa\DynamicFrontName\Model\FrontNameAction;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\FrameWork\Event\ManagerInterface;

/**
* Class FrontNameGenerator
*
* @package Alaa\DynamicFrontName\Cron
* @author Alaa Al-Maliki <alaa.almaliki@gmail.com>
*/
class FrontNameGenerator
{
/**
* @var FrontNameAction
*/
protected $frontNameAction;

/**
* @var Logger
*/
protected $logger;

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var ManagerInterface
*/
protected $eventManager;

/**
* FrontNameGenerator constructor.
*
* @param FrontNameAction $frontNameAction
* @param Logger $logger
* @param ScopeConfigInterface $scopeConfig
* @param ManagerInterface $eventManager
*/
public function __construct(
FrontNameAction $frontNameAction,
Logger $logger,
ScopeConfigInterface $scopeConfig,
ManagerInterface $eventManager
) {
$this->frontNameAction = $frontNameAction;
$this->logger = $logger;
$this->scopeConfig = $scopeConfig;
$this->eventManager = $eventManager;
}

/**
* Change admin front name
*/
public function run()
{
if ($this->scopeConfig->isSetFlag('dynamic_frontname/settings/enabled')) {
try {
$frontName = $this->frontNameAction->changeFrontName();
if (null !== $frontName) {
$this->eventManager
->dispatch('backend_front_name_changed', ['front_name' => $frontName]);
}
} catch (FrontNameCreateException $e) {
$this->logger->critical($e->getMessage());
}
}
}
}
13 changes: 13 additions & 0 deletions Exception/FrontNameCreateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Alaa\DynamicFrontName\Exception;

/**
* Class FrontNameCreateException
*
* @package Alaa\DynamicFrontName\Exception
* @author Alaa Al-Maliki <alaa.almaliki@gmail.com>
*/
class FrontNameCreateException extends \Exception
{
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Alaa Al-Maliki <alaa.almaliki@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions Logger/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Alaa\DynamicFrontName\Logger;

use Magento\Framework\Logger\Handler\Base;

/**
* Class Handler
*
* @package Alaa\DynamicFrontName\Logger
* @author Alaa Al-Maliki <alaa.almaliki@gmail.com>
*/
class Handler extends Base
{
/**
* @var int
*/
protected $loggerType = \Monolog\Logger::INFO;

/**
* @var string
*/
protected $fileName = '/var/log/dynamic-frontname.log';
}
13 changes: 13 additions & 0 deletions Logger/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Alaa\DynamicFrontName\Logger;

/**
* Class Logger
*
* @package Alaa\DynamicFrontName\Logger
* @author Alaa Al-Maliki <alaa.almaliki@gmail.com>
*/
class Logger extends \Monolog\Logger
{
}
43 changes: 43 additions & 0 deletions Model/AdminMailList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Alaa\DynamicFrontName\Model;

use Magento\Framework\App\ResourceConnection;

/**
* Class AdminMailList
*
* @package Alaa\DynamicFrontName\Model
* @author Alaa Al-Maliki <alaa.almaliki@gmail.com>
*/
class AdminMailList implements AdminMailListInterface
{
/**
* @var ResourceConnection
*/
protected $resourceConnection;

/**
* AdminEmailList constructor.
*
* @param ResourceConnection $resourceConnection
*/
public function __construct(ResourceConnection $resourceConnection)
{
$this->resourceConnection = $resourceConnection;
}

/**
* @return array
*/
public function getEmails(): array
{
$connection = $this->resourceConnection->getConnection();
$select = $connection->select()
->from('admin_user', ['email'])
->columns(['name' => new \Zend_Db_Expr('CONCAT(firstname, " ", lastname)')])
->where('is_active = ?', 1);

return $connection->fetchAssoc($select);
}
}
17 changes: 17 additions & 0 deletions Model/AdminMailListInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Alaa\DynamicFrontName\Model;

/**
* Interface AdminMailListInterface
*
* @package Alaa\DynamicFrontName\Model
* @author Alaa Al-Maliki <alaa.almaliki@gmail.com>
*/
interface AdminMailListInterface
{
/**
* @return array
*/
public function getEmails(): array;
}
43 changes: 43 additions & 0 deletions Model/FrontName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Alaa\DynamicFrontName\Model;

/**
* Class FrontName
*
* @package Alaa\DynamicFrontName\Model
* @author Alaa Al-Maliki <alaa.almaliki@gmail.com>
*/
class FrontName implements FrontNameInterface
{
/**
* @var string
*/
protected $frontName;

/**
* FrontName constructor.
*
* @param string $frontName
*/
public function __construct(string $frontName)
{
$this->frontName = $frontName;
}

/**
* @return string
*/
public function getFrontName(): string
{
return $this->frontName;
}

/**
* @return string
*/
public function __toString(): string
{
return $this->getFrontName();
}
}
81 changes: 81 additions & 0 deletions Model/FrontNameAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php declare(strict_types=1);

namespace Alaa\DynamicFrontName\Model;

use Alaa\DynamicFrontName\Exception\FrontNameCreateException;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;

/**
* Class FrontNameAction
*
* @package Alaa\DynamicFrontName\Model
* @author Alaa Al-Maliki <alaa.almaliki@gmail.com>
*/
class FrontNameAction
{
/**
* @var Writer
*/
protected $writer;

/**
* @var FrontNameBuilder
*/
protected $frontNameBuilder;

/**
* FrontNameAction constructor.
*
* @param Writer $writer
* @param FrontNameBuilder $frontNameBuilder
*/
public function __construct(
Writer $writer,
FrontNameBuilder $frontNameBuilder
) {
$this->writer = $writer;
$this->frontNameBuilder = $frontNameBuilder;
}

/**
* @return null|string
* @throws FrontNameCreateException
*/
public function changeFrontName()
{
$exception = null;

try {
$frontName = (string) $this->frontNameBuilder->buildFrontName();
if (is_string($frontName) && strlen($frontName) > 0) {
$this->writer->saveConfig(
[
'app_env' => [
'backend' => [
'frontName' => $frontName,
]
]
]
);

return $frontName;
}
} catch (FileSystemException $e) {
$exception = $e;
} catch (LocalizedException $e) {
$exception = $e;
}

if (null !== $exception) {
throw new FrontNameCreateException(
\sprintf('Could not create Front Name for Admin. %s', $exception->getMessage()),
1,
$exception
);
}

return null;
}
}
Loading

0 comments on commit 81d530d

Please sign in to comment.