Code Duplication    Length = 71-71 lines in 2 locations

src/widgets/filePreview/InsetDimensions.php 1 location

@@ 32-102 (lines=71) @@
29
 * $height = $size->getHeight(); // 150 (height according to the original aspect ratio)
30
 * ```
31
 */
32
class InsetDimensions implements DimensionsInterface
33
{
34
    /**
35
     * @var Dimensions
36
     */
37
    private $originalDimensions;
38
    /**
39
     * @var Dimensions
40
     */
41
    private $targetDimensions;
42
43
    /**
44
     * InsetDimensions constructor.
45
     *
46
     * @param Dimensions $originalDimensions the original image dimensions
47
     * @param Dimensions $targetDimensions the target image dimensions
48
     */
49
    public function __construct(Dimensions $originalDimensions, Dimensions $targetDimensions)
50
    {
51
        $this->originalDimensions = $originalDimensions;
52
        $this->targetDimensions = $targetDimensions;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getWidth()
59
    {
60
        if ($this->originalDimensions->getWidth() < $this->targetDimensions->getWidth()) {
61
            return $this->originalDimensions->getWidth();
62
        }
63
64
        if ($this->isVertical()) {
65
            return intval(ceil($this->targetDimensions->getWidth() * $this->originalDimensions->getRatio()));
66
        }
67
68
        return $this->targetDimensions->getWidth();
69
    }
70
71
    /**
72
     * @return int
73
     */
74
    public function getHeight()
75
    {
76
        if ($this->originalDimensions->getHeight() < $this->targetDimensions->getHeight()) {
77
            return $this->originalDimensions->getHeight();
78
        }
79
80
        if (!$this->isVertical()) {
81
            return intval(ceil($this->targetDimensions->getHeight() / $this->originalDimensions->getRatio()));
82
        }
83
84
        return $this->targetDimensions->getHeight();
85
    }
86
87
    /**
88
     * @return bool whether image is in the portrait orientation
89
     */
90
    private function isVertical()
91
    {
92
        return $this->originalDimensions->getRatio() < 1;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getRatio()
99
    {
100
        return $this->originalDimensions->getRatio();
101
    }
102
}
103

src/widgets/filePreview/OutboundDimensions.php 1 location

@@ 32-102 (lines=71) @@
29
 * $height = $size->getHeight(); // 600 (resized to keep aspect ratio)
30
 * ```
31
 */
32
class OutboundDimensions implements DimensionsInterface
33
{
34
    /**
35
     * @var Dimensions
36
     */
37
    private $originalDimensions;
38
    /**
39
     * @var Dimensions
40
     */
41
    private $targetDimensions;
42
43
    /**
44
     * OutboundDimensions constructor.
45
     *
46
     * @param Dimensions $originalDimensions the original image dimensions
47
     * @param Dimensions $targetDimensions the target image dimensions
48
     */
49
    public function __construct(Dimensions $originalDimensions, Dimensions $targetDimensions)
50
    {
51
        $this->originalDimensions = $originalDimensions;
52
        $this->targetDimensions = $targetDimensions;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getWidth()
59
    {
60
        if ($this->originalDimensions->getWidth() < $this->targetDimensions->getWidth()) {
61
            return $this->originalDimensions->getWidth();
62
        }
63
64
        if ($this->isVertical()) {
65
            return $this->targetDimensions->getWidth();
66
        }
67
68
        return $this->targetDimensions->getWidth();
69
    }
70
71
    /**
72
     * @return float|int
73
     */
74
    public function getHeight()
75
    {
76
        if ($this->originalDimensions->getHeight() < $this->targetDimensions->getHeight()) {
77
            return $this->originalDimensions->getHeight();
78
        }
79
80
        if ($this->isVertical()) {
81
            return intval(ceil($this->targetDimensions->getHeight() / $this->originalDimensions->getRatio()));
82
        }
83
84
        return intval(ceil($this->targetDimensions->getHeight() / $this->originalDimensions->getRatio()));
85
    }
86
87
    /**
88
     * @return bool whether image is in the portrait orientation
89
     */
90
    private function isVertical()
91
    {
92
        return $this->originalDimensions->getRatio() < 1;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getRatio()
99
    {
100
        return $this->originalDimensions->getRatio();
101
    }
102
}
103