Completed
Push — master ( 676136...b5d1cf )
by Mathieu
02:39
created

LayerEffectTrait::y()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Image\Effect;
4
5
use \InvalidArgumentException;
6
7
/**
8
 * Full implementation, as a Trait, of the Layer Effect Interface.
9
 */
10
trait LayerEffectTrait
11
{
12
    /**
13
     * @var float $opacity
14
     */
15
    private $opacity = 1.0;
16
17
    /**
18
     * @var string $gravity
19
     */
20
    private $gravity = 'nw';
21
22
    /**
23
     * Horizontal adjustment, in pixels.
24
     * Negative values will move mask to the left, positive values to the right.
25
     * Depends on the gravity setting
26
     * @var integer $x
27
     */
28
    private $x = 0;
29
30
    /**
31
     * Vertical adjustment, in pixels.
32
     * Negative values will move mask to the top, positive values to the bottom.
33
     * Depends on the gravity setting
34
     * @var integer $y
35
     */
36
    private $y = 0;
37
38
    /**
39
     * @param float $opacity The mask opacity.
40
     * @throws InvalidArgumentException If the mask opacity is not a numeric value or not between 0.0 and 1.0.
41
     * @return AbstractMaskEffect Chainable
42
     */
43
    public function setOpacity($opacity)
44
    {
45
        if (!is_numeric($opacity) || ($opacity < 0) || ( $opacity > 1)) {
46
            throw new InvalidArgumentException(
47
                'Opacity must be a float between 0.0 and 1.0'
48
            );
49
        }
50
        $this->opacity = (float)$opacity;
51
        return $this;
52
    }
53
54
    /**
55
     * @return float
56
     */
57
    public function opacity()
58
    {
59
        return $this->opacity;
60
    }
61
62
    /**
63
     * @param string $gravity The mask gravity.
64
     * @throws InvalidArgumentException If the argument is not a valid gravity name.
65
     * @return AbstractMaskEffect Chainable
66
     */
67
    public function setGravity($gravity)
68
    {
69
        if (!in_array($gravity, $this->image()->availableGravities())) {
0 ignored issues
show
Bug introduced by
It seems like image() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
70
            throw new InvalidArgumentException(
71
                'Gravity is not valid'
72
            );
73
        }
74
        $this->gravity = $gravity;
75
        return $this;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function gravity()
82
    {
83
        return $this->gravity;
84
    }
85
86
    /**
87
     * @param integer $x The mask X position.
88
     * @throws InvalidArgumentException If the position is not a numeric value.
89
     * @return AbstractMaskEffect Chainable
90
     */
91
    public function setX($x)
92
    {
93
        if (!is_numeric($x)) {
94
            throw new InvalidArgumentException(
95
                'X must be a an integer (in pixel)'
96
            );
97
        }
98
        $this->x = (int)$x;
99
        return $this;
100
    }
101
102
    /**
103
     * @return float
104
     */
105
    public function x()
106
    {
107
        return $this->x;
108
    }
109
110
    /**
111
     * @param integer $y The Y position.
112
     * @throws InvalidArgumentException If the position is not a numeric value.
113
     * @return AbstractMaskEffect Chainable
114
     */
115
    public function setY($y)
116
    {
117
        if (!is_numeric($y)) {
118
            throw new InvalidArgumentException(
119
                'Y must be a an integer (in pixel)'
120
            );
121
        }
122
        $this->y = (int)$y;
123
        return $this;
124
    }
125
126
    /**
127
     * @return float
128
     */
129
    public function y()
130
    {
131
        return $this->y;
132
    }
133
}
134