1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the PommProject/PommBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2016 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\PommBundle\Model; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Client\ClientPooler; |
13
|
|
|
use PommProject\ModelManager\ModelLayer\ModelLayerPooler; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
15
|
|
|
use Psr\Container\ContainerInterface as PsrContainerInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ModelLayerPooler |
19
|
|
|
* |
20
|
|
|
* Pooler for ModelLayer session client. |
21
|
|
|
* |
22
|
|
|
* @package ModelManager |
23
|
|
|
* @copyright 2014 - 2016 Grégoire HUBERT |
24
|
|
|
* @author Miha Vrhovnik |
25
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
26
|
|
|
* @see ClientPooler |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
class ContainerModelLayerPooler extends ModelLayerPooler implements ServiceMapInterface |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
private $serviceMap = []; |
31
|
|
|
/** @var PsrContainerInterface|ContainerInterface */ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function addModelToServiceMapping($class, $serviceId) |
38
|
|
|
{ |
39
|
|
|
$this->serviceMap[$class] = $serviceId; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
protected function createClient($class) |
46
|
|
|
{ |
47
|
|
|
if (array_key_exists($class, $this->serviceMap)) { |
48
|
|
|
return $this->container->get($this->serviceMap[$class]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return parent::createClient($class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param PsrContainerInterface|ContainerInterface $container |
56
|
|
|
*/ |
57
|
|
|
public function setContainer($container) |
58
|
|
|
{ |
59
|
|
|
if (!($container instanceof PsrContainerInterface) && !($container instanceof ContainerInterface)) { |
60
|
|
|
throw new \InvalidArgumentException('$contaner should be instance of Symfony\Component\DependencyInjection\ContainerInterface or Psr\Container\ContainerInterface'); |
61
|
|
|
} |
62
|
|
|
$this->container = $container; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.