Completed
Push — master ( 1a4c19...c1db7b )
by Ben
02:15
created

PdfWriterOptions::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * BaconPdf
4
 *
5
 * @link      http://github.com/Bacon/BaconPdf For the canonical source repository
6
 * @copyright 2015 Ben Scholzen (DASPRiD)
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Bacon\Pdf\Options;
11
12
use Bacon\Pdf\Encryption\AbstractEncryption;
13
use Bacon\Pdf\Encryption\NullEncryption;
14
use Bacon\Pdf\Exception\DomainException;
15
16
final class PdfWriterOptions
17
{
18
    /**
19
     * @var string
20
     */
21
    private $pdfVersion;
22
23
    /**
24
     * @var EncryptionOptions|null
25
     */
26
    private $encryptionOptions;
27
28
    /**
29
     * @param  string $pdfVersion
30
     * @throws DomainException
31
     */
32
    public function __construct($pdfVersion = '1.7')
33
    {
34
        if (!in_array($pdfVersion, ['1.3', '1.4', '1.5', '1.6', '1.7'])) {
35
            throw new DomainException('PDF version is not in the supported range (1.3 - 1.7)');
36
        }
37
38
        $this->pdfVersion = $pdfVersion;
39
    }
40
41
    /**
42
     * Returns the PDF version to use for the document.
43
     *
44
     * @return string
45
     */
46
    public function getPdfVersion()
47
    {
48
        return $this->pdfVersion;
49
    }
50
51
    /**
52
     * Sets encryption options.
53
     *
54
     * @param EncryptionOptions $encryptionOptions
55
     */
56
    public function setEncryptionOptions(EncryptionOptions $encryptionOptions)
57
    {
58
        $this->encryptionOptions = $encryptionOptions;
59
    }
60
61
    /**
62
     * @param  string $permanentFileIdentifier
63
     * @return NullEncryption
64
     */
65
    public function getEncryption($permanentFileIdentifier)
66
    {
67
        if (null === $this->encryptionOptions) {
68
            return new NullEncryption();
69
        }
70
71
        return AbstractEncryption::forPdfVersion($this->pdfVersion, $permanentFileIdentifier, $this->encryptionOptions);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Bacon\Pdf\Encryp...is->encryptionOptions); (Bacon\Pdf\Encryption\Pdf11Encryption) is incompatible with the return type documented by Bacon\Pdf\Options\PdfWriterOptions::getEncryption of type Bacon\Pdf\Encryption\NullEncryption.

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...
72
    }
73
}
74