ObjectPool::setObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author  : Jagepard <[email protected]>
7
 * @license https://mit-license.org/ MIT
8
 */
9
10
namespace Creational\ObjectPool;
11
12
final class ObjectPool
13
{
14
    private array $pool = [];
15
16
    /**
17
     * Gets an object
18
     * --------------
19
     * Получает объект
20
     * 
21
     * @param  string          $name
22
     * @return ObjectInterface
23
     */
24
    public function getObject(string $name): ObjectInterface
25
    {
26
        if (array_key_exists($name, $this->pool)) {
27
            return $this->pool[$name];
28
        }
29
30
        throw new \InvalidArgumentException("Object $name does'nt exist");
31
    }
32
33
    /**
34
     * Adds an object
35
     * --------------
36
     * Добавляет объект
37
     * 
38
     * @param  ObjectInterface $object
39
     * @return void
40
     */
41
    public function setObject(ObjectInterface $object): void
42
    {
43
        if (array_key_exists($object->getName(), $this->pool)) {
44
            throw new \InvalidArgumentException("Object {$object->getName()} is already exist");
45
        }
46
47
        $this->pool[$object->getName()] = $object;
48
    }
49
}
50