Completed
Pull Request — master (#106)
by Litera
08:36
created

SkautisService::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services\SkautIS;
4
5
use Nette\Object;
6
use Skautis\Skautis;
7
8
/**
9
 * SkautIS service
10
 */
11
abstract class SkautisService extends Object
12
{
13
14
	/**
15
	 * Class Table reference
16
	 * @var instance of BaseTable
17
	 */
18
	protected $table;
19
20
21
	/**
22
	 * Holds SkautIS instance
23
	 * @var Skautis\Skautis
24
	 */
25
	protected $skautis;
26
27
28
	/**
29
	 * Use local storage (cache)?
30
	 * @var bool
31
	 */
32
	private $useCache = TRUE;
33
34
35
	/**
36
	 * Short term storage for saving SkautIS answers
37
	 * @var type
38
	 */
39
	private static $storage;
40
41
42
	/**
43
	 * Construct
44
	 */
45
	public function __construct(Skautis $skautIS = NULL)
46
	{
47
		$this->setSkautis($skautIS);
0 ignored issues
show
Bug introduced by
It seems like $skautIS defined by parameter $skautIS on line 45 can be null; however, App\Services\SkautIS\SkautisService::setSkautis() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
48
		self::$storage = array();
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<App\Services\SkautIS\type> of property $storage.

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...
49
	}
50
51
52
	/**
53
	 * Get user information
54
	 *
55
	 * @param   void
56
	 * @return  array  Login ID, Role ID, Unit ID
57
	 */
58
	public function getInfo()
59
	{
60
		$skautis = $this->getSkautis();
61
62
		return [
63
			"ID_Login" => $skautis->getUser()->getLoginId(),
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ID_Login does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
64
			"ID_Role"  => $skautis->getUser()->getRoleId(),
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ID_Role does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
65
			"ID_Unit"  => $skautis->getUser()->getUnitId(),
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ID_Unit does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
66
		];
67
	}
68
69
70
	/**
71
	 * Save value to local storage
72
	 *
73
	 * @param   mixed  $id
74
	 * @param   mixed  $val
75
	 * @return  mixed
76
	 */
77
	protected function save($id, $val)
78
	{
79
		if ($this->useCache) {
80
			self::$storage[$id] = $val;
81
		}
82
		return $val;
83
	}
84
85
86
	/**
87
	 * Get object from local storage
88
	 *
89
	 * @param   string|int   $id
90
	 * @return  mixed|FALSE
91
	 */
92
	protected function load($id)
93
	{
94
		if ($this->useCache && array_key_exists($id, self::$storage)) {
95
			return self::$storage[$id];
96
		}
97
		return FALSE;
98
	}
99
100
	/**
101
	 * @return Skautis\Skautis
102
	 */
103
	public function getSkautis(): Skautis
104
	{
105
		return $this->skautis;
106
	}
107
108
	/**
109
	 * @param Skautis\Skautis $skautis
110
	 *
111
	 * @return self
112
	 */
113
	public function setSkautis(Skautis $skautis): self
114
	{
115
		$this->skautis = $skautis;
0 ignored issues
show
Documentation Bug introduced by
It seems like $skautis of type object<Skautis\Skautis> is incompatible with the declared type object<Skautis\Skautis\Skautis> of property $skautis.

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...
116
117
		return $this;
118
	}
119
120
}
121