ConsignmentServiceCollection   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 20%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
c 1
b 1
f 0
lcom 1
cbo 3
dl 0
loc 41
ccs 3
cts 15
cp 0.2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addConsignmentService() 0 5 1
A findConsignmentService() 0 15 2
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 = preg_replace('/[^a-zA-Z0-9 ]+/i', ' ', $manufacturer->getName());
39
        $name = Container::camelize($name).'ConsignmentService';
40
41
        if (!isset($this->consignmentServices[$name])) {
42
            throw new NonExistentConsignmentServiceException('Consignment service `'.$name.'` not found. Did you mean any of these? '.join(', ', array_keys($this->consignmentServices)));
43
        }
44
45
        /** @var ConsignmentServiceInterface $service */
46
        $service = $this->consignmentServices[$name];
47
        $service->setManufacturer($manufacturer);
48
49
        return $service;
50
    }
51
}
52