GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Resource::setSubResources()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 5
eloc 11
nc 7
nop 1
1
<?php
2
3
namespace prgTW\BaseCRM\Resource;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use prgTW\BaseCRM\Transport\Transport;
7
8
abstract class Resource extends BaseResource
9
{
10
	/** @var \Resource[] */
11
	protected $subResources = [];
12
13
	/** @var Transport */
14
	protected $transport;
15
16
	/** @var string */
17
	protected $uri;
18
19
	/**
20
	 * @param Transport $transport
21
	 * @param string    $uri
22
	 * @param array     $data
23
	 */
24
	public function __construct(Transport $transport, $uri, array $data = [])
25
	{
26
		$this->transport = $transport;
27
		$this->uri       = $uri;
28
		if ([] !== $data)
29
		{
30
			$this->hydrate($data);
31
		}
32
33
		$this->init();
34
	}
35
36
	/**
37
	 * @return string
38
	 */
39
	abstract protected function getEndpoint();
40
41
	protected function init()
42
	{
43
44
	}
45
46
	/**
47
	 * @param string $suffix
48
	 *
49
	 * @return string
50
	 */
51
	protected function getFullUri($suffix = '')
52
	{
53
		return sprintf('%s/%s/%s%s', static::getEndpoint(), static::PREFIX, $this->uri, $suffix);
54
	}
55
56
	/**
57
	 * @param string $name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
58
	 *
59
	 * @return Resource|\Resource[]
0 ignored issues
show
Documentation introduced by
Should the return type not be \Resource|null|\Resource[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
	 */
61
	public function getSubResources($name = null)
62
	{
63
		if (isset($name))
64
		{
65
			return isset($this->subResources[$name]) ? $this->subResources[$name] : null;
66
		}
67
68
		return $this->subResources;
69
	}
70
71
	/**
72
	 * @param string[] $resourceClassNames
73
	 *
74
	 * @throws \InvalidArgumentException when class name is not a Resource
75
	 */
76
	protected function setSubResources(array $resourceClassNames)
77
	{
78
		foreach ($resourceClassNames as $key => $value)
79
		{
80
			$resourceClassName = is_int($key) ? $value : $key;
81
			if (false === is_subclass_of($resourceClassName, Resource::class))
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \prgTW\BaseCRM\Resource\Resource::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
82
			{
83
				//@codeCoverageIgnoreStart
84
				throw new \InvalidArgumentException(sprintf('Class %s is not an instance of %s', $resourceClassName, Resource::class));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
85
				//@codeCoverageIgnoreEnd
86
			}
87
			$resourceName = $this->getResourceName($resourceClassName);
88
			$uri          = ltrim(sprintf('%s/%s', $this->uri, $resourceName), '/');
89
			if (false === is_int($key))
90
			{
91
				$uri = $value;
92
			}
93
94
			$class = new $resourceClassName($this->transport, $uri);
95
96
			$this->subResources[lcfirst(Inflector::camelize($resourceName))] = $class;
97
		}
98
	}
99
100
	/**
101
	 * @param array  $params
102
	 * @param string $instanceClassName
0 ignored issues
show
Documentation introduced by
Should the type for parameter $instanceClassName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
103
	 * @param string $idParam
104
	 *
105
	 * @return Resource
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
106
	 */
107
	protected function getObjectFromArray(array $params, $instanceClassName = null, $idParam = 'id')
108
	{
109
		if (null === $instanceClassName)
110
		{
111
			$instanceClassName = Inflector::singularize(static::class);
112
		}
113
114
		if (array_key_exists($idParam, $params))
115
		{
116
			$uri = sprintf('%s/%s', $this->uri, $params[$idParam]);
117
		}
118
		else
119
		{
120
			$uri = $this->uri;
121
		}
122
123
		return new $instanceClassName($this->transport, $uri, $params);
124
	}
125
126
	/**
127
	 * @param $method
128
	 * @param $arguments
129
	 *
130
	 * @return Resource|ResourceCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be \Resource|\Resource[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
131
	 * @throws \BadMethodCallException
132
	 */
133
	public function __call($method, $arguments)
134
	{
135
		if ('get' !== substr($method, 0, 3))
136
		{
137
			//@codeCoverageIgnoreStart
138
			throw new \BadMethodCallException('Unknown resource');
139
			//@codeCoverageIgnoreEnd
140
		}
141
142
		$key         = lcfirst(substr($method, 3));
143
		$subResource = $this->getSubResources($key);
144
		if (null === $subResource)
145
		{
146
			//@codeCoverageIgnoreStart
147
			throw new \BadMethodCallException(sprintf('Resource "%s" not found', $key));
148
			//@codeCoverageIgnoreEnd
149
		}
150
151
		return $subResource;
152
	}
153
154
	/**
155
	 * @return Transport
156
	 */
157
	public function getTransport()
158
	{
159
		return $this->transport;
160
	}
161
}
162