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

Timestamp::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 5
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
namespace Isign\Document;
3
4
use Isign\DocumentTypeProvider;
5
use Isign\FileFieldsTrait;
6
use Isign\QueryInterface;
7
use Isign\ResultInterface;
8
use Symfony\Component\Validator\Constraints as Assert;
9
10
/**
11
 * Class Timestamp
12
 * Timestamps document's signatures.
13
 *
14
 * @package Isign\Document
15
 */
16 View Code Duplication
class Timestamp implements QueryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    use FileFieldsTrait;
19
20
    /** @var string Possible values: pdf, adoc, mdoc */
21
    private $type;
22
23
    /** @var string file path */
24
    private $path;
25
26
    /**
27
     * @param string $type Possible values: pdf, adoc, mdoc
28
     * @param string $path
29
     */
30 6
    public function __construct($type, $path)
31
    {
32 6
        $this->type = $type;
33 6
        $this->path = $path;
34 6
    }
35
36
    /**
37
     * API action name, part of full API request url
38
     * @return string
39
     */
40 1
    public function getAction()
41
    {
42 1
        return 'timestamp';
43
    }
44
45
    /**
46
     * Field and values association used in query
47
     * @return array
48
     */
49 2
    public function getFields()
50
    {
51
        return [
52 2
            'type' => $this->type,
53 2
            'file' => $this->getFileFields($this->path),
54
        ];
55
    }
56
57
    /**
58
     * Result object for this query result
59
     * @return ResultInterface
60
     */
61 1
    public function createResult()
62
    {
63 1
        return new TimestampResult();
64
    }
65
66
    /**
67
     * Validation constraints for fields
68
     * @return array
69
     */
70 1
    public function getValidationConstraints()
71
    {
72 1
        return new Assert\Collection([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...aints\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...
73 1
            'type'  => new Assert\Required([
74 1
                new Assert\NotBlank(),
75 1
                new Assert\Choice([
76 1
                    'choices' => DocumentTypeProvider::getPrimaryDocumentTypes()
77
                ])
78
            ]),
79 1
            'file' => new Assert\Collection([
80 1
                'name' => new Assert\Required([
81 1
                    new Assert\NotBlank()
82
                ]),
83 1
                'content' => new Assert\Required([
84 1
                    new Assert\NotBlank(),
85
                ]),
86 1
                'digest' => new Assert\Required([
87 1
                    new Assert\NotBlank()
88
                ]),
89
            ]),
90
        ]);
91
    }
92
93
    /**
94
     * HTTP method to use
95
     * @return string
96
     */
97 1
    public function getMethod()
98
    {
99 1
        return QueryInterface::POST;
100
    }
101
}
102