Source of file Client.php
Size: 7,175 Bytes - Last Modified: 2021-01-19T05:03:31+00:00
C:/Users/MAKS/Code/_PROJECTS/amqp-agent/src/Client.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
Covered by 11 test(s):
91
Covered by 11 test(s):
92
Covered by 2 test(s):
93
Covered by 1 test(s):
9495
Covered by 1 test(s):
96
Covered by 1 test(s):
979899
Covered by 11 test(s):
100101102103104105106107108109110
Covered by 7 test(s):
111112113114115116117118119120121122
Covered by 8 test(s):
123124
Covered by 8 test(s):
125
Covered by 7 test(s):
126127128
Covered by 1 test(s):
129
Covered by 1 test(s):
130
Covered by 1 test(s):
131132133134135136137138139140141142
Covered by 2 test(s):
143
Covered by 2 test(s):
144
Covered by 2 test(s):
145146
Covered by 2 test(s):
147
Covered by 2 test(s):
148
Covered by 2 test(s):
149
Covered by 2 test(s):
150
Covered by 2 test(s):
151
Covered by 2 test(s):
152153154155156157158
Covered by 2 test(s):
159160161162163164165166167168169
Covered by 1 test(s):
170
Covered by 1 test(s):
171
Covered by 1 test(s):
172
Covered by 1 test(s):
173
Covered by 1 test(s):
174
Covered by 1 test(s):
175
Covered by 1 test(s):
176
Covered by 1 test(s):
177
Covered by 1 test(s):
178179180181
Covered by 1 test(s):
182183184185186187188189190
Covered by 1 test(s):
191
Covered by 1 test(s):
192
Covered by 1 test(s):
193
Covered by 1 test(s):
194
Covered by 1 test(s):
195
Covered by 1 test(s):
196
Covered by 1 test(s):
197
Covered by 1 test(s):
198199200201
Covered by 1 test(s):
202203204205206207208209210
Covered by 1 test(s):
211
Covered by 1 test(s):
212
Covered by 1 test(s):
213
Covered by 1 test(s):
214215216217
Covered by 1 test(s):
218219220221222223224225226
Covered by 1 test(s):
227
Covered by 1 test(s):
228
Covered by 1 test(s):
229
Covered by 1 test(s):
230231232233
Covered by 1 test(s):
234235236237238239240241242
Covered by 1 test(s):
243
Covered by 1 test(s):
244245246
Covered by 1 test(s):
247248249250251252253254255256
Covered by 2 test(s):
257
Covered by 2 test(s):
258259260
Covered by 2 test(s):
261262263264265266267268269
Covered by 1 test(s):
270271272
| <?php /** * @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com> * @copyright Marwan Al-Soltany 2020 * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ declare(strict_types=1); namespace MAKS\AmqpAgent; use MAKS\AmqpAgent\Config; use MAKS\AmqpAgent\Worker\Publisher; use MAKS\AmqpAgent\Worker\Consumer; use MAKS\AmqpAgent\RPC\ClientEndpoint; use MAKS\AmqpAgent\RPC\ServerEndpoint; use MAKS\AmqpAgent\Helper\ArrayProxy; use MAKS\AmqpAgent\Helper\Serializer; use MAKS\AmqpAgent\Helper\Logger; use MAKS\AmqpAgent\Exception\AmqpAgentException; /** * A class returns everything AMQP Agent has to offer. A simple service container so to say. * * Example: * ``` * $config = new Config('path/to/some/config-file.php'); * $client = new Client($config); * $publisher = $client->getPublisher(); // or $client->get('publisher'); * $consumer = $client->getConsumer(); // or $client->get('consumer'); * ``` * * @since 1.0.0 * @api */ class Client { /** * An instance of the configuration object. * @var Config */ protected $config; /** * An instance of the Publisher class. * @var Publisher */ protected $publisher; /** * An instance of the Consumer class. * @var Consumer */ protected $consumer; /** * An instance of the RPC Client class. * @var ClientEndpoint */ protected $clientEndpoint; /** * An instance of the RPC Server class. * @var ServerEndpoint */ protected $serverEndpoint; /** * An instance of the Serializer class. * @var Serializer */ protected $serializer; /** * An instance of the Logger class. * @var Logger */ protected $logger; /** * Client object constructor. * @param Config|string $config An instance of the Config class or a path to a config file. * @throws AmqpAgentException */ public function __construct($config) { if ($config instanceof Config) { $this->config = $config; } elseif (is_string($config) && strlen(trim($config)) > 0) { $this->config = new Config($config); } else { throw new AmqpAgentException( 'A Config instance or a valid path to a config file must be specified.' ); } } /** * Gets a class member via public property access notation. * @param string $member Property name. * @return mixed * @throws AmqpAgentException */ public function __get(string $member) { // using $this->get() to reuse the logic in get() method. return $this->get($member); } /** * Returns an instance of a class by its name (lowercase, UPPERCASE, PascalCase, camelCase, dot.case, kebab-case, or snake_case representation of class name). * @param string $member Member name. Check out `self::gettable()` for available members. * @return Config|Publisher|Consumer|Serializer|Logger * @throws AmqpAgentException */ public function get(string $member) { $method = __FUNCTION__ . preg_replace('/[\.\-_]+/', '', ucwords(strtolower($member), '.-_')); if (method_exists($this, $method)) { return $this->{$method}(); } $available = ArrayProxy::castArrayToString($this->gettable()); throw new AmqpAgentException( "The requested member with the name \"{$member}\" does not exist! Available members are: {$available}." ); } /** * Returns an array of available members that can be obtained via `self::get()`. * @since 1.2.1 * @return array */ public static function gettable(): array { $methods = get_class_methods(static::class); $gettable = []; $separator = ('.-_')[rand(0, 2)]; foreach ($methods as $method) { if (preg_match('/get[A-Z][a-z]+/', $method)) { $gettable[] = strtolower( preg_replace( ['/get/', '/([a-z])([A-Z])/'], ['', '$1' . $separator . '$2'], $method ) ); } } return $gettable; } /** * Returns an instance of the Publisher class. * @return Publisher * @api */ public function getPublisher(): Publisher { if (!isset($this->publisher)) { $this->publisher = new Publisher( $this->config->connectionOptions, $this->config->channelOptions, $this->config->queueOptions, $this->config->exchangeOptions, $this->config->bindOptions, $this->config->messageOptions, $this->config->publishOptions ); } return $this->publisher; } /** * Returns an instance of the Consumer class. * @return Consumer */ public function getConsumer(): Consumer { if (!isset($this->consumer)) { $this->consumer = new Consumer( $this->config->connectionOptions, $this->config->channelOptions, $this->config->queueOptions, $this->config->qosOptions, $this->config->waitOptions, $this->config->consumeOptions ); } return $this->consumer; } /** * Returns an instance of the RPC Client class. * @return ClientEndpoint */ public function getClientEndpoint(): ClientEndpoint { if (!isset($this->clientEndpoint)) { $this->clientEndpoint = new ClientEndpoint( $this->config->rpcConnectionOptions, $this->config->rpcQueueName ); } return $this->clientEndpoint; } /** * Returns an instance of the RPC Server class. * @return ServerEndpoint */ public function getServerEndpoint(): ServerEndpoint { if (!isset($this->serverEndpoint)) { $this->serverEndpoint = new ServerEndpoint( $this->config->rpcConnectionOptions, $this->config->rpcQueueName ); } return $this->serverEndpoint; } /** * Returns an instance of the Serializer class. * @return Serializer */ public function getSerializer(): Serializer { if (!isset($this->serializer)) { $this->serializer = new Serializer(); } return $this->serializer; } /** * Returns an instance of the Logger class. * Filename and directory must be set through setters. * @return Logger */ public function getLogger(): Logger { if (!isset($this->logger)) { $this->logger = new Logger(null, null); } return $this->logger; } /** * Returns the currently used config object. * @return Config */ public function getConfig(): Config { return $this->config; } } |