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

Permissions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4286
cc 1
eloc 17
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Encryption;
11
12
/**
13
 * Permissions as defined in table 3.20 in section 3.5.
14
 */
15
final class Permissions
16
{
17
    /**
18
     * @var bool
19
     */
20
    private $mayPrint;
21
22
    /**
23
     * @var bool
24
     */
25
    private $mayPrintHighResolution;
26
27
    /**
28
     * @var bool
29
     */
30
    private $mayModify;
31
32
    /**
33
     * @var bool
34
     */
35
    private $mayCopy;
36
37
    /**
38
     * @var bool
39
     */
40
    private $mayAnnotate;
41
42
    /**
43
     * @var bool
44
     */
45
    private $mayFillInForms;
46
47
    /**
48
     * @var bool
49
     */
50
    private $mayExtractForAccessibility;
51
52
    /**
53
     * @var bool
54
     */
55
    private $mayAssemble;
56
57
    /**
58
     * @param bool $mayPrint
59
     * @param bool $mayPrintHighResolution
60
     * @param bool $mayModify
61
     * @param bool $mayCopy
62
     * @param bool $mayAnnotate
63
     * @param bool $mayFillInForms
64
     * @param bool $mayExtractForAccessibility
65
     * @param bool $mayAssemble
66
     */
67
    public function __construct(
68
        $mayPrint,
69
        $mayPrintHighResolution,
70
        $mayModify,
71
        $mayCopy,
72
        $mayAnnotate,
73
        $mayFillInForms,
74
        $mayExtractForAccessibility,
75
        $mayAssemble
76
    ) {
77
        $this->mayPrint                   = $mayPrint;
78
        $this->mayPrintHighResolution     = $mayPrintHighResolution;
79
        $this->mayModify                  = $mayModify;
80
        $this->mayCopy                    = $mayCopy;
81
        $this->mayAnnotate                = $mayAnnotate;
82
        $this->mayFillInForms             = $mayFillInForms;
83
        $this->mayExtractForAccessibility = $mayExtractForAccessibility;
84
        $this->mayAssemble                = $mayAssemble;
85
    }
86
87
    /**
88
     * Creates permissions which allow nothing.
89
     *
90
     * @return self
91
     */
92
    public static function allowNothing()
93
    {
94
        return new self(false, false, false, false, false, false, false, false);
95
    }
96
97
    /**
98
     * Creates permissions which allow everything.
99
     *
100
     * @return self
101
     */
102
    public static function allowEverything()
103
    {
104
        return new self(true, true, true, true, true, true, true, true);
105
    }
106
107
    /**
108
     * Convert the permissions to am integer bit mask.
109
     *
110
     * @internal Keep in mind that the bit positions named in the PDF reference are counted from 1, while in here they
111
     *           are counted from 0.
112
     *
113
     * @param  int $revision
114
     * @return int
115
     */
116
    public function toInt($revision)
117
    {
118
        $flags = 0;
119
120
        if ($this->mayPrint) {
121
            $flags |= (1 << 2);
122
        }
123
124
        if ($this->mayModify) {
125
            $flags |= (1 << 3);
126
        }
127
128
        if ($this->mayCopy) {
129
            $flags |= (1 << 4);
130
        }
131
132
        if ($this->mayAnnotate) {
133
            $flags |= (1 << 5);
134
        }
135
136
        if ($revision >= 3) {
137
            if ($this->mayFillInForms) {
138
                $flags |= (1 << 8);
139
            }
140
141
            if ($this->mayExtractForAccessibility) {
142
                $flags |= (1 << 9);
143
            }
144
145
            if ($this->mayAssemble) {
146
                $flags |= (1 << 10);
147
            }
148
149
            if ($this->mayPrintHighResolution) {
150
                $flags |= (1 << 11);
151
            }
152
        }
153
154
        return $flags;
155
    }
156
}
157