Token::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * primaERP - Token handling Class.
4
 *
5
 * @author     Vítězslav Dvořák <[email protected]>
6
 * @copyright  (C) 2017 Vitex Software
7
 */
8
9
namespace primaERP;
10
11
/**
12
 * Token handling Class.
13
 *
14
 * @url http://devdoc.primaerp.com/rest/authentication.html
15
 */
16
class Token extends Authentication
17
{
18
    /**
19
     * Saves obejct instace (singleton...).
20
     *
21
     * @var Shared
22
     */
23
    private static $_instance = null;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
24
25
    /**
26
     * Token
27
     *
28
     * @param mixed $init
29
     * @param array $options
30
     */
31 1
    public function __construct($init = null, $options = array())
32
    {
33 1
        parent::__construct($init, $options);
34 1
        $this->refreshToken();
35 1
    }
36
37 1
    public function authentication()
38
    {
39 1
        $this->defaultUrlParams['apikey'] = $this->apikey;
40 1
    }
41
42
    /**
43
     * Current Token String
44
     *
45
     * @return string
46
     */
47 1
    public function getTokenString()
48
    {
49 1
        if ($this->isTokenExpired()) {
50
            $this->refreshToken();
51
        }
52 1
        return $this->getDataValue('token');
53
    }
54
55
56
    
57 1
    public function takeData($data)
58
    {
59 1
        if (isset($data['expiration']) && preg_match('/([0-9]+)/',
60 1
                $data['expiration'], $expire)) {
61 1
            $data['expiration'] = intval($expire[1]);
62
        }
63 1
        return parent::takeData($data);
64
    }
65
66
    /**
67
     * Check Access Token expiration state
68
     *
69
     * @return boolean
70
     */
71 1
    public function isTokenExpired()
72
    {
73 1
        $expireTime = $this->getDataValue('expiration') - time();
74 1
        return $expireTime < 5;
75
    }
76
77 1
    public function requestFreshToken()
78
    {
79 1
        return $this->requestData('login', 'GET');
80
    }
81
82 1
    public function refreshToken()
83
    {
84 1
        $this->takeData($this->requestFreshToken());
85 1
    }
86
87
    /**
88
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako konstruktor)
89
     * se bude v ramci behu programu pouzivat pouze jedna jeho Instance (ta prvni).
90
     *
91
     * @param string $class název třídy jenž má být zinstancována
0 ignored issues
show
Bug introduced by
There is no parameter named $class. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
92
     *
93
     * @link   http://docs.php.net/en/language.oop5.patterns.html Dokumentace a priklad
94
     *
95
     * @return \Ease\Shared
96
     */
97 1
    public static function singleton()
98
    {
99 1
        if (!isset(self::$_instance)) {
100 1
            $class           = __CLASS__;
101 1
            self::$_instance = new $class();
102
        }
103
104 1
        return self::$_instance;
105
    }
106
107
    /**
108
     * Vrací se.
109
     *
110
     * @return Shared
111
     */
112 1
    public static function &instanced()
113
    {
114 1
        $tokener = self::singleton();
115
116 1
        return $tokener;
117
    }
118
}
119