Completed
Pull Request — develop (#9)
by
unknown
03:41
created

MobileHash::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 1
1
<?php
2
namespace Isign\Sign;
3
4
use Isign\DocumentTypeProvider;
5
use Isign\FileFieldsTrait;
6
use Isign\QueryInterface;
7
use Isign\Validator\Constraints\Code;
8
use Isign\Validator\Constraints\Phone;
9
use Symfony\Component\Validator\Constraints as Assert;
10
11
/**
12
 * Sign document using mobile ID hash.
13
 */
14
class MobileHash implements QueryInterface
15
{
16
    /** @var string user phone number */
17
    private $phone;
18
19
    /** @var string user personal code */
20
    private $code;
21
    
22
    /** @var string Hash to sign */
23
    private $hash;
24
    
25
    /** @var string Hash algorithm */
26
    private $hashAlgorithm;
27
    
28
    /** @var string Signature encryption type. */
29
    private $encryption;
30
    
31
    /** @var string Message displayed on the phone screen. Attention: UTF-8 symbols are not allowed */
32
    private $message;
33
34
    /** @var string Language for messages displayed on the phone screen */
35
    private $language;
36
37
    /**
38
     * @param string $phone
39
     * @param string $code
40
     * @param string $hash
41
     * @param string $hashAlgorithm
42
     * @param string $encryption
43
     * @param string $message
44
     * @param string $language
45
     */
46 5
    public function __construct(
47
        $phone,
48
        $code,
49
        $hash,
50
        $hashAlgorithm,
51
        $encryption,
52
        $message,
53
        $language
54
    ) {
55 5
        $this->phone = $phone;
56 5
        $this->code = $code;
57 5
        $this->hash = $hash;
58 5
        $this->hashAlgorithm = $hashAlgorithm;
59 5
        $this->encryption = $encryption;
60 5
        $this->message = $message;
61 5
        $this->language = $language;
62 5
    }
63
64
    /**
65
     * Field and values association used in query
66
     * @return array
67
     */
68 1
    public function getFields()
69
    {
70
        return [
71 1
            'phone' => $this->phone,
72 1
            'code' => $this->code,
73 1
            'hash' => $this->hash,
74 1
            'hash_algorithm' => $this->hashAlgorithm,
75 1
            'encryption' => $this->encryption,
76 1
            'message' => $this->message,
77 1
            'language' => $this->language,
78 1
        ];
79
    }
80
81
    /**
82
     * Validation constraints for request data validation
83
     * @return Assert\Collection
84
     */
85 1
    public function getValidationConstraints()
86
    {
87 1
        return new Assert\Collection([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...nstraints\NotBlank())); (Symfony\Component\Validator\Constraints\Collection) is incompatible with the return type declared by the interface Isign\QueryInterface::getValidationConstraints of type Isign\Symfony\Component\...\Constraints\Collection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
88 1
            'phone' => new Assert\Required([
89 1
                new Assert\NotBlank(),
90 1
                new Phone(),
91 1
            ]),
92 1
            'code' => new Assert\Required([
93 1
                new Assert\NotBlank(),
94 1
                new Code()
95 1
            ]),
96 1
            'hash' => new Assert\NotBlank(),
97 1
            'hash_algorithm' => new Assert\NotBlank(),
98 1
            'encryption' => new Assert\NotBlank(),
99 1
            'message' => new Assert\NotBlank(),
100 1
            'language' => new Assert\NotBlank(),
101 1
        ]);
102
    }
103
104
    /**
105
     * Result object for this query result
106
     * @return MobileHashResult
107
     */
108 1
    public function createResult()
109
    {
110 1
        return new MobileHashResult();
111
    }
112
113
    /**
114
     * API action name, part of full API request url
115
     * @return string
116
     */
117 1
    public function getAction()
118
    {
119 1
        return 'mobile/sign/hash';
120
    }
121
122
    /**
123
     * HTTP method to use
124
     * @return string
125
     */
126 1
    public function getMethod()
127
    {
128 1
        return QueryInterface::POST;
129
    }
130
}
131