-
-
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.
Implementing connectors factory (#1)
* Implementing connectors factory
- Loading branch information
Showing
24 changed files
with
451 additions
and
127 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 |
---|---|---|
|
@@ -18,4 +18,4 @@ | |
Devices module | ||
""" | ||
|
||
__version__ = "0.66.0" | ||
__version__ = "0.67.0" |
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
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,66 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* ConnectorFactory.php | ||
* | ||
* @license More in license.md | ||
* @copyright https://www.fastybird.com | ||
* @author Adam Kadlec <adam.kadlec@fastybird.com> | ||
* @package FastyBird:DevicesModule! | ||
* @subpackage Connectors | ||
* @since 0.67.0 | ||
* | ||
* @date 03.07.22 | ||
*/ | ||
|
||
namespace FastyBird\DevicesModule\Connectors; | ||
|
||
use FastyBird\DevicesModule\Exceptions; | ||
use FastyBird\Metadata\Entities as MetadataEntities; | ||
use SplObjectStorage; | ||
|
||
/** | ||
* Connector factory | ||
* | ||
* @package FastyBird:DevicesModule! | ||
* @subpackage Connectors | ||
* | ||
* @author Adam Kadlec <adam.kadlec@fastybird.com> | ||
*/ | ||
final class ConnectorFactory | ||
{ | ||
|
||
/** @var SplObjectStorage<IConnectorFactory, null> */ | ||
private SplObjectStorage $factories; | ||
|
||
/** | ||
* @param IConnectorFactory[] $factories | ||
*/ | ||
public function __construct( | ||
array $factories | ||
) { | ||
$this->factories = new SplObjectStorage(); | ||
|
||
foreach ($factories as $factory) { | ||
$this->factories->attach($factory); | ||
} | ||
} | ||
|
||
/** | ||
* @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector | ||
* | ||
* @return IConnector | ||
*/ | ||
public function create(MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector): IConnector | ||
{ | ||
/** @var IConnectorFactory $factory */ | ||
foreach ($this->factories as $factory) { | ||
if ($connector->getType() === $factory->getType()) { | ||
return $factory->create($connector); | ||
} | ||
} | ||
|
||
throw new Exceptions\InvalidStateException('Connector could not be created'); | ||
} | ||
|
||
} |
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
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); | ||
|
||
/** | ||
* IConnectorFactory.php | ||
* | ||
* @license More in license.md | ||
* @copyright https://www.fastybird.com | ||
* @author Adam Kadlec <adam.kadlec@fastybird.com> | ||
* @package FastyBird:DevicesModule! | ||
* @subpackage Connectors | ||
* @since 0.67.0 | ||
* | ||
* @date 03.07.22 | ||
*/ | ||
|
||
namespace FastyBird\DevicesModule\Connectors; | ||
|
||
use FastyBird\Metadata\Entities as MetadataEntities; | ||
|
||
/** | ||
* Connector factory interface | ||
* | ||
* @package FastyBird:DevicesModule! | ||
* @subpackage Connectors | ||
* | ||
* @author Adam Kadlec <adam.kadlec@fastybird.com> | ||
*/ | ||
interface IConnectorFactory | ||
{ | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getType(): string; | ||
|
||
/** | ||
* @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector | ||
* | ||
* @return IConnector | ||
*/ | ||
public function create(MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector): IConnector; | ||
|
||
} |
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
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
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
Oops, something went wrong.