Completed
Push — master ( 8803de...e3dc66 )
by Andrii
03:17
created

ServiceTrait::removeToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Library for confirmation tokens
5
 *
6
 * @link      https://github.com/hiqdev/php-confirmator
7
 * @package   php-confirmator
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\php\confirmator;
13
14
trait ServiceTrait
15
{
16 3
    public function issueToken(array $data)
17
    {
18 3
        $token = new Token($this, $data);
19 3
        $this->writeToken($token);
20
21 3
        return $token;
22
    }
23
24 1
    public function checkToken($token, array $data)
25
    {
26 1
        $token = $this->findToken($token);
27 1
        if (!$token) {
28
            return false;
29
        }
30
31 1
        return $token->check($data);
32
    }
33
34 2
    public function findToken($token)
35
    {
36 2
        if ($token instanceof Token) {
37 1
            return $token;
38
        }
39
40 2
        $data = $this->readToken($token);
41
42 2
        return empty($data) ? null : new Token($this, $data, $token);
43
    }
44
45
    public function removeToken($token)
46
    {
47
        return $this->getStorage()->remove((string) $token);
0 ignored issues
show
Bug introduced by
It seems like getStorage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
    }
49
50 2
    protected function readToken($string)
51
    {
52 2
        return $this->getStorage()->has($string) ? json_decode($this->getStorage()->get($string), true) : null;
0 ignored issues
show
Bug introduced by
It seems like getStorage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53
    }
54
55 3
    protected function writeToken(Token $token)
56
    {
57 3
        return $this->getStorage()->set((string) $token, json_encode($token->mget()));
0 ignored issues
show
Bug introduced by
It seems like getStorage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
58
    }
59
}
60