Completed
Push — master ( ea8464...30c3c3 )
by WEBEWEB
01:20
created

Image::setPathname()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Model;
13
14
use InvalidArgumentException;
15
use RuntimeException;
16
use WBW\Bundle\CoreBundle\Model\Attribute\IntegerHeightTrait;
17
use WBW\Bundle\CoreBundle\Model\Attribute\IntegerSizeTrait;
18
use WBW\Bundle\CoreBundle\Model\Attribute\IntegerWidthTrait;
19
use WBW\Bundle\CoreBundle\Model\Attribute\StringDirectoryTrait;
20
use WBW\Bundle\CoreBundle\Model\Attribute\StringExtensionTrait;
21
use WBW\Bundle\CoreBundle\Model\Attribute\StringFilenameTrait;
22
use WBW\Bundle\CoreBundle\Model\Attribute\StringPathnameTrait;
23
24
class Image {
25
26
    use IntegerHeightTrait;
27
    use IntegerSizeTrait;
28
    use IntegerWidthTrait;
29
    use StringDirectoryTrait;
30
    use StringExtensionTrait;
31
    use StringFilenameTrait;
32
    use StringPathnameTrait;
33
34
    /**
35
     * Orientation "horizontal".
36
     *
37
     * @var string
38
     */
39
    const ORIENTATION_HORIZONTAL = "horizontal";
40
41
    /**
42
     * Orientation "vertical".
43
     *
44
     * @var string
45
     */
46
    const ORIENTATION_VERTICAL = "vertical";
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param string $pathname The pathname.
52
     */
53
    public function __construct($pathname) {
54
        $this->setPathname($pathname);
55
    }
56
57
    /**
58
     * Get the dimensions.
59
     *
60
     * @return int[] Returns the dimensions.
61
     */
62
    public function getDimensions() {
63
        return [$this->getWidth(), $this->getHeight()];
64
    }
65
66
    /**
67
     * Get the orientation.
68
     *
69
     * @return string|null Returns the orientation, null if width and height are equals.
70
     */
71
    public function getOrientation() {
72
73
        if ($this->getHeight() < $this->getWidth()) {
74
            return self::ORIENTATION_HORIZONTAL;
75
        }
76
77
        if ($this->getWidth() < $this->getHeight()) {
78
            return self::ORIENTATION_VERTICAL;
79
        }
80
81
        return null;
82
    }
83
84
    /**
85
     * Initialize.
86
     *
87
     * @return void
88
     */
89
    public function init() {
90
91
        $this->setDirectory(dirname($this->getPathname()));
92
        $this->setFilename(basename($this->getPathname()));
93
94
        $parts = explode(".", $this->getFilename());
95
        $this->setExtension(end($parts));
96
97
        list($width, $height) = getimagesize($this->getPathname());
98
        $this->setHeight($height);
99
        $this->setWidth($width);
100
101
        $this->setSize(filesize($this->getPathname()));
102
    }
103
104
    /**
105
     * Resize.
106
     *
107
     * @param int $newWidth The new width.
108
     * @param int $newHeight The new height.
109
     * @param string $pathname The pathname.
110
     * @return bool Returns true in case os success, false otherwise.
111
     * @throws RuntimeException Throws a runtime exception if the re-sampled copy failed.
112
     */
113
    public function resize($newWidth, $newHeight, $pathname) {
114
115
        $this->init();
116
117
        list($w, $h) = ImageHelper::newDimensions($this, $newWidth, $newHeight);
118
119
        $input  = ImageHelper::newInputStream($this);
120
        $output = ImageHelper::newOutputStream($this, $w, $h);
121
122
        $result = imagecopyresampled($output, $input, 0, 0, 0, 0, $w, $h, $this->getWidth(), $this->getHeight());
123
        if (false === $result) {
124
            throw new RuntimeException("The re-sampled copy failed");
125
        }
126
127
        return ImageHelper::saveOutputStream($this, $output, $pathname);
0 ignored issues
show
Bug introduced by
It seems like $output defined by \WBW\Bundle\CoreBundle\M...utStream($this, $w, $h) on line 120 can also be of type null; however, WBW\Bundle\CoreBundle\Mo...per::saveOutputStream() does only seem to accept resource, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
128
    }
129
130
    /**
131
     * Set the pathname.
132
     *
133
     * @param string $pathname The pathname.
134
     * @return Image Returns this image.
135
     * @throws InvalidArgumentException Throws an invalid argument exception if the pathname was not found.
136
     */
137
    protected function setPathname($pathname) {
138
        if (false === realpath($pathname)) {
139
            throw new InvalidArgumentException(sprintf("The pathname \"%s\" was not found", $pathname));
140
        }
141
        $this->pathname = realpath($pathname);
142
        return $this;
143
    }
144
}
145