|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Cms\Service\Loader; |
|
4
|
|
|
|
|
5
|
|
|
// dependency from charcoal-factory |
|
6
|
|
|
use Charcoal\Factory\FactoryInterface; |
|
7
|
|
|
|
|
8
|
|
|
// dependency from charcoal-core |
|
9
|
|
|
use Charcoal\Loader\CollectionLoader; |
|
10
|
|
|
|
|
11
|
|
|
// dependency from php |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
use Exception; |
|
14
|
|
|
|
|
15
|
|
|
use \Charcoal\Translator\TranslatorAwareTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Abstract Loader |
|
19
|
|
|
*/ |
|
20
|
|
|
class AbstractLoader |
|
21
|
|
|
{ |
|
22
|
|
|
use TranslatorAwareTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Store the factory instance for the current class. |
|
26
|
|
|
* |
|
27
|
|
|
* @var FactoryInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $modelFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Store the collection loader for the current class. |
|
33
|
|
|
* |
|
34
|
|
|
* @var CollectionLoader |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $collectionLoader; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var object $objType The object to load. |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $objType; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* NewsLoader constructor. |
|
45
|
|
|
* @param array $data The Data. |
|
46
|
|
|
* @throws Exception When there is missing data. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(array $data) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset($data['factory'])) { |
|
51
|
|
|
throw new Exception(sprintf( |
|
52
|
|
|
'Model Factory must be defined in the %s constructor.', |
|
53
|
|
|
get_called_class() |
|
54
|
|
|
)); |
|
55
|
|
|
} |
|
56
|
|
|
if (!isset($data['loader'])) { |
|
57
|
|
|
throw new Exception(sprintf( |
|
58
|
|
|
'CollectionLoader must be defined in the %s constructor.', |
|
59
|
|
|
get_called_class() |
|
60
|
|
|
)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (isset($data['translator'])) { |
|
64
|
|
|
$this->setTranslator($data['translator']); |
|
65
|
|
|
} |
|
66
|
|
|
$this->setModelFactory($data['factory']); |
|
67
|
|
|
$this->setCollectionLoader($data['loader']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set an object model factory. |
|
72
|
|
|
* |
|
73
|
|
|
* @param FactoryInterface $factory The model factory, to create objects. |
|
74
|
|
|
* @return self |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function setModelFactory(FactoryInterface $factory) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->modelFactory = $factory; |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Retrieve the object model factory. |
|
85
|
|
|
* |
|
86
|
|
|
* @throws RuntimeException If the model factory was not previously set. |
|
87
|
|
|
* @return FactoryInterface |
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
public function modelFactory() |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
if (!isset($this->modelFactory)) { |
|
92
|
|
|
throw new RuntimeException( |
|
93
|
|
|
sprintf('Model Factory is not defined for "%s"', get_class($this)) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->modelFactory; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set a model collection loader. |
|
102
|
|
|
* |
|
103
|
|
|
* @param CollectionLoader $loader The collection loader. |
|
104
|
|
|
* @return self |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function setCollectionLoader(CollectionLoader $loader) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->collectionLoader = $loader; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Retrieve the model collection loader. |
|
115
|
|
|
* |
|
116
|
|
|
* @throws RuntimeException If the collection loader was not previously set. |
|
117
|
|
|
* @return CollectionLoader |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function collectionLoader() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
if (!isset($this->collectionLoader)) { |
|
122
|
|
|
throw new RuntimeException( |
|
123
|
|
|
sprintf('Collection Loader is not defined for "%s"', get_class($this)) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->collectionLoader; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.