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

ServiceTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 46
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A issueToken() 0 7 1
A checkToken() 0 9 2
A findToken() 0 10 3
A removeToken() 0 4 1
A readToken() 0 4 2
A writeToken() 0 4 1
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