Completed
Push — master ( 7db5b7...e686f3 )
by Daniel
03:06
created

FontTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 160
Duplicated Lines 8.75 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 17
c 2
b 1
f 0
lcom 2
cbo 1
dl 14
loc 160
ccs 45
cts 45
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
getArgumentValue() 0 1 ?
setArgument() 0 1 ?
getPdfSettings() 0 1 ?
A getCannotEmbedFontPolicy() 14 14 3
A setCannotEmbedFontPolicy() 0 11 2
A isEmbedAllFonts() 0 14 3
A setEmbedAllFonts() 0 6 2
A getMaxSubsetPct() 0 9 2
A setMaxSubsetPct() 0 6 1
A isSubsetFonts() 0 9 2
A setSubsetFonts() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the Ghostscript package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Ghostscript\Device\DistillerParameters;
9
10
use GravityMedia\Ghostscript\Enum\CannotEmbedFontPolicy;
11
use GravityMedia\Ghostscript\Enum\PdfSettings;
12
13
/**
14
 * The font distiller parameters trait
15
 *
16
 * @package GravityMedia\Ghostscript\Device\DistillerParameters
17
 */
18
trait FontTrait
19
{
20
    /**
21
     * Get argument value
22
     *
23
     * @param string $name
24
     *
25
     * @return null|string
26
     */
27
    abstract protected function getArgumentValue($name);
28
29
    /**
30
     * Set argument
31
     *
32
     * @param string $argument
33
     *
34
     * @return $this
35
     */
36
    abstract protected function setArgument($argument);
37
38
    /**
39
     * Get PDF settings
40
     *
41
     * @return string
42
     */
43
    abstract public function getPdfSettings();
44
45
    /**
46
     * Get cannot embed font policy
47
     *
48
     * @return string
49
     */
50 15 View Code Duplication
    public function getCannotEmbedFontPolicy()
0 ignored issues
show
Duplication introduced by
This method 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...
51
    {
52 15
        $value = $this->getArgumentValue('-dCannotEmbedFontPolicy');
53 15
        if (null === $value) {
54 15
            switch ($this->getPdfSettings()) {
55 15
                case PdfSettings::PREPRESS:
56 3
                    return CannotEmbedFontPolicy::ERROR;
57 12
                default:
58 12
                    return CannotEmbedFontPolicy::WARNING;
59 12
            }
60
        }
61
62 15
        return ltrim($value, '/');
63
    }
64
65
    /**
66
     * Set cannot embed font policy
67
     *
68
     * @param string $cannotEmbedFontPolicy
69
     *
70
     * @param \InvalidArgumentException
71
     *
72
     * @return $this
73
     */
74 18
    public function setCannotEmbedFontPolicy($cannotEmbedFontPolicy)
75
    {
76 18
        $cannotEmbedFontPolicy = ltrim($cannotEmbedFontPolicy, '/');
77 18
        if (!in_array($cannotEmbedFontPolicy, CannotEmbedFontPolicy::values())) {
78 3
            throw new \InvalidArgumentException('Invalid cannot embed font policy argument');
79
        }
80
81 15
        $this->setArgument(sprintf('-dCannotEmbedFontPolicy=/%s', $cannotEmbedFontPolicy));
82
83 15
        return $this;
84
    }
85
86
    /**
87
     * Whether to embed all fonts
88
     *
89
     * @return bool
90
     */
91 15
    public function isEmbedAllFonts()
92
    {
93 15
        $value = $this->getArgumentValue('-dEmbedAllFonts');
94 15
        if (null === $value) {
95 15
            switch ($this->getPdfSettings()) {
96 15
                case PdfSettings::SCREEN:
97 3
                    return false;
98 12
                default:
99 12
                    return true;
100 12
            }
101
        }
102
103 15
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
104
    }
105
106
    /**
107
     * Set embed all fonts flag
108
     *
109
     * @param string $embedAllFonts
110
     *
111
     * @return $this
112
     */
113 15
    public function setEmbedAllFonts($embedAllFonts)
114
    {
115 15
        $this->setArgument(sprintf('-dEmbedAllFonts=%s', $embedAllFonts ? 'true' : 'false'));
116
117 15
        return $this;
118
    }
119
120
    /**
121
     * Get max subset pct
122
     *
123
     * @return int
124
     */
125 3
    public function getMaxSubsetPct()
126
    {
127 3
        $value = $this->getArgumentValue('-dMaxSubsetPct');
128 3
        if (null === $value) {
129 3
            return 100;
130
        }
131
132 3
        return intval($value);
133
    }
134
135
    /**
136
     * Set max subset pct
137
     *
138
     * @param int $maxSubsetPct
139
     *
140
     * @return $this
141
     */
142 3
    public function setMaxSubsetPct($maxSubsetPct)
143
    {
144 3
        $this->setArgument(sprintf('-dMaxSubsetPct=%s', $maxSubsetPct));
145
146 3
        return $this;
147
    }
148
149
    /**
150
     * Whether to subset fonts
151
     *
152
     * @return bool
153
     */
154 3
    public function isSubsetFonts()
155
    {
156 3
        $value = $this->getArgumentValue('-dSubsetFonts');
157 3
        if (null === $value) {
158 3
            return true;
159
        }
160
161 3
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
162
    }
163
164
    /**
165
     * Set subset fonts flag
166
     *
167
     * @param bool $subsetFonts
168
     *
169
     * @return $this
170
     */
171 3
    public function setSubsetFonts($subsetFonts)
172
    {
173 3
        $this->setArgument(sprintf('-dSubsetFonts=%s', $subsetFonts ? 'true' : 'false'));
174
175 3
        return $this;
176
    }
177
}
178