Completed
Pull Request — develop (#14)
by Rimas
03:51
created

Seal::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Isign\Document;
3
4
use Isign\DocumentTypeProvider;
5
use Isign\FileFieldsTrait;
6
use Isign\QueryInterface;
7
use Symfony\Component\Validator\Constraints as Assert;
8
9
/**
10
 * Seal a signed document.
11
 */
12
class Seal implements QueryInterface
13
{
14
    use FileFieldsTrait;
15
16
    /** @var  string document type */
17
    private $type;
18
19
    /** @var  boolean add document timestamp */
20
    private $timestamp;
21
22
    /** @var  array document to be signed */
23
    private $document;
24
25
    /**
26
     * @param string $type string document type
27
     * @param array $document document to be signed
28
     * @param boolean $timestamp boolean add document timestamp
29
     */
30 7
    public function __construct(
31
        $type,
32
        array $document,
33
        $timestamp
34
    ) {
35 7
        $this->type = $type;
36 7
        $this->timestamp = $timestamp;
37 7
        $this->document = $document;
38 7
    }
39
40
    /**
41
     * Get fields
42
     * @return array
43
     */
44 3
    public function getFields()
45
    {
46 3
        $params = $this->document;
47
48 3
        if (isset($params['files'])) {
49 3
            $params['files'] = $this->getMultipleFileFields($params['files']);
50
        }
51
52
        $return = [
53 2
            'type' => $this->type,
54 2
            $this->type => $params
55
        ];
56
57 2
        if ($this->timestamp !== null) {
58 2
            $return['timestamp'] = $this->timestamp;
59
        }
60
61 2
        return $return;
62
    }
63
64
    /**
65
     * Validation constraints for request data validation
66
     * @return Assert\Collection
67
     */
68 1
    public function getValidationConstraints()
69
    {
70 1
        return new Assert\Collection([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...\Collection(array()))); (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...
71 1
            'type'  => new Assert\Required([
72 1
                new Assert\NotBlank(),
73 1
                new Assert\Choice([
74 1
                    'choices' => DocumentTypeProvider::getAllDocumentTypes()
75
                ])
76
            ]),
77 1
            'timestamp' => new Assert\Required([
78 1
                new Assert\Type([
79 1
                    'type' => 'bool'
80
                ])
81
            ]),
82 1
            $this->type => new Assert\Collection([]),
83
        ]);
84
    }
85
86
    /**
87
     * Result object for this query result
88
     * @return SealResult
89
     */
90 1
    public function createResult()
91
    {
92 1
        return new SealResult();
93
    }
94
95
    /**
96
     * API action name, part of full url
97
     * @return string
98
     */
99 1
    public function getAction()
100
    {
101 1
        return 'seal';
102
    }
103
104
    /**
105
     * HTTP method to use
106
     * @return string
107
     */
108 1
    public function getMethod()
109
    {
110 1
        return QueryInterface::POST;
111
    }
112
}
113