System::setup()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
cc 4
eloc 9
c 3
b 2
f 1
nc 8
nop 0
dl 0
loc 21
rs 9.0534
1
<?php namespace Algorit\Synchronizer\Request;
2
3
use ReflectionClass;
4
use Illuminate\Container\Container;
5
use Illuminate\Filesystem\Filesystem;
6
use Algorit\Synchronizer\Request\Methods\Requests;
7
use Algorit\Synchronizer\Request\Contracts\SystemInterface;
8
use Algorit\Synchronizer\Request\Contracts\ResourceInterface;
9
10
abstract class System implements SystemInterface {
11
12
	/**
13
	 * The system name
14
	 *
15
	 * @var string
16
	 */
17
	public $name;
18
19
	/**
20
	 * The system namespace
21
	 *
22
	 * @var string
23
	 */
24
	public $namespace;
25
26
	/**
27
	 * The resource config path
28
	 *
29
	 * @var string
30
	 */
31
	public $path;
32
33
	/**
34
	 * The request method
35
	 *
36
	 * @var object
37
	 */
38
	protected $method;
39
40
	/**
41
	 * Create a new instance.
42
	 *
43
	 * @param  void
44
	 * @return void
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...
45
	 */
46
	public function __construct($resource = false)
47
	{
48
		if($resource)
49
		{
50
			$this->resource = $resource;
0 ignored issues
show
Bug introduced by
The property resource does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
		}
52
53
		$this->setup();
54
	}
55
56
	public function setResource(ResourceInterface $resource)
57
	{
58
		$this->resource = $resource;
59
60
		return $this;
61
	}
62
63
	public function getResource()
64
	{
65
		return $this->resource;
66
	}
67
	
68
	/**
69
	 * Setup the names.
70
	 *
71
	 * @param  void
72
	 * @return \Algorit\Synchronizer\Request\Contracts\SystemInterface
73
	 */
74
	protected function setup()
75
	{
76
		$reflector = new ReflectionClass(get_class($this));
77
78
		if( ! $this->path)
79
		{
80
			$this->path = dirname($reflector->getFileName());
81
		}
82
83
		if( ! $this->name)
84
		{
85
			$this->name = $reflector->getName();
86
		}
87
88
		if( ! $this->namespace)
89
		{
90
			$this->namespace = $reflector->getNamespaceName();
91
		}
92
93
		return $this;
94
	}
95
96
	/**
97
	 * Set the request Class.
98
	 *
99
	 * @param  string $name
100
	 * @return \Algorit\Synchronizer\Request\Contracts\SystemInterface
101
	 */
102
	public function setRequest($name = 'Request')
103
	{
104
		if(strpos($name, '\\') === false)
105
		{
106
			$name = $this->namespace . '\\' . $name;
107
		}
108
109
		$this->request = $name;
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
110
111
		return $this;
112
	}
113
114
	/**
115
	 * Load the request Class
116
	 *
117
	 * @param  \Algorit\Synchronizer\Container  $container
118
	 * @return \Algorit\Synchronizer\Request\Contracts\RequestInterface
119
	 */
120
	public function loadRequest(Container $container)
121
	{
122
		if( ! isset($this->request))
123
		{
124
			$this->setRequest();
125
		}
126
127
		return $this->request = $this->bindedContainer($container)
128
									 ->make($this->request);
129
	}
130
131
	/**
132
	 * Get the container with all its bindings
133
	 *
134
	 * @param  \Algorit\Synchronizer\Container  $container
135
	 * @return \Algorit\Synchronizer\Container  $container
136
	 */
137
	private function bindedContainer(Container $container)
138
	{
139
		// Set actual namespace in container.
140
		$container->namespace = $this->namespace;
141
142
		$container->bind('Algorit\Synchronizer\Request\Methods\MethodInterface', function()
143
		{
144
			return $this->method ?: new Requests;
145
		});
146
147
		// Is it binding the Container to itself? Brain fuck
148
		// Think we should use an interface here
149
		$container->bind('Algorit\Synchronizer\Container', function() use ($container)
150
		{
151
			return $container;
152
		});
153
154
		return $container;
155
	}
156
157
}