-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 81d530d
Showing
36 changed files
with
1,567 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.