BatchOperationCollection::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the sauls/object-registry-bundle package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Bundle\ObjectRegistryBundle\Collection;
14
15
use Sauls\Bundle\ObjectRegistryBundle\Batch\Operation\OperationInterface;
16
use Sauls\Bundle\ObjectRegistryBundle\Exception\UnsupportedBatchOperationException;
17
use Sauls\Component\Collection\ArrayCollection;
18
19
class BatchOperationCollection extends ArrayCollection implements BatchOperationCollectionInterface
20
{
21
    /**
22
     * @param $key
23
     * @param OperationInterface $value
24
     */
25 2
    public function set($key, $value): void
26
    {
27 2
        $this->checkValueIsValidOperationObject($value);
28
29 1
        parent::set($value->getName(), $value);
30 1
    }
31
32
    /**
33
     * @param $value
34
     */
35 2
    private function checkValueIsValidOperationObject($value): void
36
    {
37 2
        if ($this->isNotOperationObject($value)) {
38 1
            throw new UnsupportedBatchOperationException(
39 1
                sprintf(
40 1
                    'Given batch operation of class `%s` should implement %s interface',
41 1
                    \get_class($value),
42 1
                    OperationInterface::class)
43
            );
44
        }
45 1
    }
46
47 2
    private function isNotOperationObject($value): bool
48
    {
49 2
        return false === \is_subclass_of($value, OperationInterface::class);
50
    }
51
}
52