Completed
Push — master ( 90d78c...a53f78 )
by Андрей
07:14 queued 04:39
created

ResourceLoaderService::loadResourceForFixture()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/doctrine-fixture-module
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\DoctrineFixtureModule\ResourceLoader;
7
8
use Doctrine\Fixture\Fixture;
9
10
/**
11
 * Class ResourceLoaderService
12
 *
13
 * @package Nnx\DoctrineFixtureModule\ResourceLoader
14
 */
15
class ResourceLoaderService implements ResourceLoaderServiceInterface
16
{
17
18
    /**
19
     * Ключем является имя класса фикстуры, а значением, конфиг описыайющи загрузчик ресурсов
20
     *
21
     * @var array
22
     */
23
    protected $classFixtureToResourceLoader = [];
24
25
    /**
26
     * Менеджер плагинов, отвечающиз за загрузку ресурсов для фикстур
27
     *
28
     * @var ResourceLoaderManagerInterface
29
     */
30
    protected $resourceLoaderManager;
31
32
    /**
33
     * ResourceLoaderService constructor.
34
     *
35
     * @param ResourceLoaderManagerInterface $resourceLoaderManager
36
     */
37
    public function __construct(ResourceLoaderManagerInterface $resourceLoaderManager)
38
    {
39
        $this->setResourceLoaderManager($resourceLoaderManager);
40
    }
41
42
    /**
43
     * Возвращает массив ключем которого является имя класса фикстуры, а значением, конфиг описыайющи загрузчик ресурсов
44
     *
45
     * @return array
46
     */
47
    public function getClassFixtureToResourceLoader()
48
    {
49
        return $this->classFixtureToResourceLoader;
50
    }
51
52
    /**
53
     * Возвращает массив ключем которого является имя класса фикстуры, а значением, конфиг описыайющи загрузчик ресурсов
54
     *
55
     * @param array $classFixtureToResourceLoader
56
     *
57
     * @return $this
58
     */
59
    public function setClassFixtureToResourceLoader(array $classFixtureToResourceLoader = [])
60
    {
61
        $this->classFixtureToResourceLoader = $classFixtureToResourceLoader;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Возвращает менеджер плагинов, отвечающих за загрузку ресурсов для фикстур
68
     *
69
     * @return ResourceLoaderManagerInterface
70
     */
71
    public function getResourceLoaderManager()
72
    {
73
        return $this->resourceLoaderManager;
74
    }
75
76
    /**
77
     * Устанавливает менеджер плагинов, отвечающих за загрузку ресурсов для фикстур
78
     *
79
     * @param ResourceLoaderManagerInterface $resourceLoaderManager
80
     *
81
     * @return $this
82
     */
83
    public function setResourceLoaderManager($resourceLoaderManager)
84
    {
85
        $this->resourceLoaderManager = $resourceLoaderManager;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @inheritDoc
92
     * @throws \Nnx\DoctrineFixtureModule\ResourceLoader\Exception\InvalidFixtureResourceLoaderConfigException
93
     */
94
    public function loadResourceForFixture(Fixture $fixture)
95
    {
96
        $classFixture = get_class($fixture);
97
98
        $classFixtureToResourceLoader = $this->getClassFixtureToResourceLoader();
99
        if (array_key_exists($classFixture, $classFixtureToResourceLoader)) {
100
            $fixtureResourceLoaderConfig = $this->buildFixtureResourceLoaderConfig($classFixtureToResourceLoader[$classFixture]);
101
            $fixtureResourceLoader = $this->getResourceLoaderManager()->get($fixtureResourceLoaderConfig['name'], $fixtureResourceLoaderConfig['options']);
0 ignored issues
show
Unused Code introduced by
The call to ResourceLoaderManagerInterface::get() has too many arguments starting with $fixtureResourceLoaderConfig['options'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
102
            $fixtureResourceLoader->loadResourceForFixture($fixture);
103
        }
104
    }
105
106
    /**
107
     * Подготавливает данные необходимые для получения загрузчика ресурсов фикстуры
108
     *
109
     * @param mixed $fixtureResourceLoaderConfigData
110
     *
111
     * @throws \Nnx\DoctrineFixtureModule\ResourceLoader\Exception\InvalidFixtureResourceLoaderConfigException
112
     *
113
     * @return array
114
     */
115
    public function buildFixtureResourceLoaderConfig($fixtureResourceLoaderConfigData = null)
116
    {
117
        $fixtureResourceLoaderConfig = [];
118
119
        if (!is_array($fixtureResourceLoaderConfigData)) {
120
            $errMsg = 'Fixture resource loader config is not array';
121
            throw new Exception\InvalidFixtureResourceLoaderConfigException($errMsg);
122
        }
123
124
        if (!array_key_exists('name', $fixtureResourceLoaderConfigData)) {
125
            $errMsg = 'Resource loader name not defined';
126
            throw new Exception\InvalidFixtureResourceLoaderConfigException($errMsg);
127
        }
128
        $fixtureResourceLoaderConfig['name'] = (string)$fixtureResourceLoaderConfigData['name'];
129
130
        $fixtureResourceLoaderConfig['options'] = [];
131
        if (array_key_exists('options', $fixtureResourceLoaderConfigData)) {
132
            $fixtureResourceLoaderConfig['options'] = (array)$fixtureResourceLoaderConfigData['options'];
133
        }
134
135
        return $fixtureResourceLoaderConfig;
136
    }
137
}
138