Completed
Push — master ( 89bf6a...4ff99e )
by Joachim
07:05
created

findConsignmentService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\DandomainConsignmentBundle\ConsignmentService;
6
7
use Loevgaard\DandomainConsignmentBundle\Exception\NonExistentConsignmentServiceException;
8
use Loevgaard\DandomainFoundation\Entity\Generated\ManufacturerInterface;
9
use Symfony\Component\DependencyInjection\Container;
10
11
class ConsignmentServiceCollection
12
{
13
    /**
14
     * @var ConsignmentServiceInterface[]
15
     */
16
    protected $consignmentServices;
17
18 3
    public function __construct()
19
    {
20 3
        $this->consignmentServices = [];
21 3
    }
22
23
    public function addConsignmentService(ConsignmentServiceInterface $consignmentService): void
24
    {
25
        $class = (new \ReflectionClass($consignmentService))->getShortName();
26
        $this->consignmentServices[$class] = $consignmentService;
27
    }
28
29
    /**
30
     * @param ManufacturerInterface $manufacturer
31
     *
32
     * @return ConsignmentServiceInterface
33
     *
34
     * @throws NonExistentConsignmentServiceException
35
     */
36
    public function findConsignmentService(ManufacturerInterface $manufacturer): ConsignmentServiceInterface
37
    {
38
        $name = Container::camelize($manufacturer->getName()).'ConsignmentService';
39
40
        if (!isset($this->consignmentServices[$name])) {
41
            throw new NonExistentConsignmentServiceException('Consignment service `'.$name.'` not found. Did you mean any of these? '.join(', ', array_keys($this->consignmentServices)));
42
        }
43
44
        /** @var ConsignmentServiceInterface $service */
45
        $service = $this->consignmentServices[$name];
46
        $service->setManufacturer($manufacturer);
47
48
        return $service;
49
    }
50
}
51