Loader   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 20
Bugs 8 Features 7
Metric Value
c 20
b 8
f 7
dl 0
loc 199
rs 10
wmc 14
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setSystem() 0 6 1
A getSystem() 0 4 1
A getBuilder() 0 4 1
A getRequest() 0 4 1
A loadSystem() 0 11 1
A select() 0 19 4
A startCollection() 0 9 2
A start() 0 21 2
1
<?php namespace Algorit\Synchronizer;
2
3
use Closure;
4
use Exception;
5
use Psr\Log\LoggerInterface;
6
use Illuminate\Support\Collection;
7
use Illuminate\Container\Container;
8
use Algorit\Synchronizer\Request\Config;
9
use Algorit\Synchronizer\Request\Contracts\SystemInterface;
10
use Algorit\Synchronizer\Request\Contracts\ResourceInterface;
11
use Algorit\Synchronizer\Request\Contracts\ContainerInterface;
12
13
class Loader {
14
15
	use LoggerTrait;
16
17
	/**
18
	 * The Company instance.
19
	 *
20
	 * @var object
21
	 */
22
	protected $company;
23
24
	/**
25
	 * The Request instance.
26
	 *
27
	 * @var object
28
	 */
29
	protected $request;
30
31
	/**
32
	 * The System instance.
33
	 *
34
	 * @var object
35
	 */
36
	protected $system;
37
38
	/**
39
	 * The Builder instance.
40
	 *
41
	 * @var \Algorit\Synchronizer\Builder
42
	 */
43
	protected $builder;
44
45
	/**
46
	 * The Container instance.
47
	 *
48
	 * @var \Algorit\Synchronizer\Container
49
	 */
50
	protected $container;
51
52
	/**
53
	* The Config instance.
54
	*
55
	* @var \Algorit\Synchronizer\Config
56
	*/
57
	public $config;
58
59
	/**
60
	 * Create a new Loader.
61
	 *
62
	 * @param  Builder $builder
63
	 * @param  Config  $config
64
	 * @return instance
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
65
	 */
66
	public function __construct(Container $container, Builder $builder, Config $config)
67
	{
68
		$this->config    = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type object<Algorit\Synchronizer\Request\Config> is incompatible with the declared type object<Algorit\Synchronizer\Config> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
		$this->builder   = $builder;
70
		$this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container of type object<Illuminate\Container\Container> is incompatible with the declared type object<Algorit\Synchronizer\Container> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
	}
72
73
	/**
74
	 * Set the System instance
75
	 *
76
	 * @param  \Algorit\Synchronizer\Request\Contracts\SystemInterface $system
77
	 * @return \Algorit\Synchronizer\Loader
78
	 */
79
	public function setSystem(SystemInterface $system)
80
	{
81
		$this->system = $system;
82
83
		return $this;
84
	}
85
86
	/**
87
	 * Get the System instance
88
	 *
89
	 * @param  void
90
	 * @return \Algorit\Synchronizer\System
91
	 */
92
	public function getSystem()
93
	{
94
		return $this->system;
95
	}
96
97
	/**
98
	 * Get the Builder instance
99
	 *
100
	 * @param  void
101
	 * @return \Algorit\Synchronizer\Builder
102
	 */
103
	public function getBuilder()
104
	{
105
		return $this->builder;
106
	}
107
108
	/**
109
	 * Get the System instance
110
	 *
111
	 * @param  void
112
	 * @return \Algorit\Synchronizer\Request\Request
113
	 */
114
	public function getRequest()
115
	{
116
		return $this->request;
117
	}
118
119
	/**
120
	 * Load the ERP Request.
121
	 *
122
	 * @param  \Algorit\Synchronizer\Request\Contracts\SystemInterface $system
123
	 * @param  mixed  $callback
124
	 * @return \Algorit\Synchronizer\Loader
125
	 */
126
	public function loadSystem(SystemInterface $system, $callback = false)
127
	{
128
		$this->logger->notice('Loading ' . $system->name);
0 ignored issues
show
Bug introduced by
Accessing name on the interface Algorit\Synchronizer\Req...ntracts\SystemInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
129
130
		$this->setSystem($system);
131
132
		// Load system
133
		$this->request = $this->system->loadRequest($this->container);
134
135
		return $this->select($system->getResource(), $callback);
136
	}
137
138
	/**
139
	 * Test if the variable is a collection or a resource.
140
	 *
141
	 * @param  mixed $resource
142
	 * @param  mixed $callback
143
	 * @return \Algorit\Synchronizer\Loader
144
	 */
145
	private function select($resource, $callback)
146
	{
147
		if($resource == false)
148
		{
149
			throw new Exception('Resource is not defined.');
150
		}
151
152
		if($resource instanceof Collection)
153
		{
154
			return $this->startCollection($resource, $callback);
155
		}
156
157
		if($resource instanceof ResourceInterface)
158
		{
159
			return $this->start($resource, $callback);
160
		}
161
162
		return $this;
163
	}
164
165
	/**
166
	 * Start the loader given a Collection as resource.
167
	 *
168
	 * @param  \Illuminate\Support\Collection $collection
169
	 * @param  mixed $callback
170
	 * @return \Algorit\Synchronizer\Loader
171
	 */
172
	public function startCollection(Collection $collection, $callback = false)
173
	{
174
		foreach($collection as $resource)
175
		{
176
			$this->start($resource, $callback);
177
		}
178
179
		return $this;
180
	}
181
182
	/**
183
	 * Set the resource and start the builder.
184
	 *
185
	 * @param  \Algorit\Synchronizer\Request\Contracts\ResourceInterface $resource
186
	 * @param  mixed $callback
187
	 * @return \Algorit\Synchronizer\Loader
188
	 */
189
	public function start(ResourceInterface $resource, $callback = false)
190
	{
191
		// Set config
192
		$this->request->setConfig($this->config->setup($this->system, $resource));
193
194
		// Set system resource
195
		$this->request->setResource($resource);
196
197
		// Set logger
198
		$this->builder->setLogger($this->logger);
199
200
		// Start the Builder
201
		$this->builder->start($this->request, $resource);
202
203
		if($callback instanceof Closure)
204
		{
205
			return $callback($this); // return?
206
		}
207
208
		return $this;
209
	}
210
211
}
212