DataManagerFactory::getManagers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebTheory\Saveyour\Factory;
4
5
use Exception;
6
use WebTheory\Factory\Traits\ClassResolverTrait;
7
use WebTheory\Factory\Traits\SmartFactoryTrait;
8
use WebTheory\Saveyour\Contracts\Data\FieldDataManagerInterface;
9
use WebTheory\Saveyour\Contracts\Factory\FieldDataManagerResolverFactoryInterface;
10
use WebTheory\Saveyour\Data\FieldDataManagerCallback;
11
12
class DataManagerFactory implements FieldDataManagerResolverFactoryInterface
13
{
14
    use SmartFactoryTrait;
0 ignored issues
show
Bug introduced by
The trait WebTheory\Factory\Traits\SmartFactoryTrait requires the property $name which is not provided by WebTheory\Saveyour\Factory\DataManagerFactory.
Loading history...
15
    use ClassResolverTrait;
16
17
    private array $managers = [];
18
19
    protected array $namespaces = [];
20
21
    public const NAMESPACES = [
22
        'webtheory.saveyour' => "WebTheory\\Saveyour\\Managers",
23
    ];
24
25
    public const MANAGERS = [
26
        'callback' => FieldDataManagerCallback::class,
27
    ];
28
29
    protected const CONVENTION = '%sFieldDataManager';
30
31 3
    public function __construct(array $namespaces = [], array $managers = [])
32
    {
33 3
        $this->namespaces = $namespaces + static::NAMESPACES;
34 3
        $this->managers = $managers + static::MANAGERS;
35
    }
36
37
    public function getManagers()
38
    {
39
        return $this->managers;
40
    }
41
42
    /**
43
     * @return $this
44
     */
45
    public function addManager(string $arg, string $manager): DataManagerFactory
46
    {
47
        $this->managers[$arg] = $manager;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return $this
54
     */
55
    public function addManagers(array $managers): DataManagerFactory
56
    {
57
        $this->managers = $managers + $this->managers;
58
59
        return $this;
60
    }
61
62 3
    public function create(string $manager, array $args = []): FieldDataManagerInterface
63
    {
64 3
        $manager = $this->managers[$manager] ?? null;
65
66 3
        if (isset($manager)) {
67 3
            $manager = $this->build($manager, $args);
68
        } elseif ($class = $this->getClass($manager)) {
0 ignored issues
show
Bug introduced by
$manager of type null is incompatible with the type string expected by parameter $arg of WebTheory\Saveyour\Facto...agerFactory::getClass(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        } elseif ($class = $this->getClass(/** @scrutinizer ignore-type */ $manager)) {
Loading history...
69
            $manager = $this->build($class, $args);
70
        } else {
71
            throw new Exception("{$manager} is not a recognized data manager");
72
        }
73
74 3
        return $manager;
75
    }
76
}
77