ImagesStorage   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 45
c 4
b 0
f 0
dl 0
loc 126
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getImageByName() 0 10 3
A deleteImageByName() 0 20 5
A getImages() 0 3 1
A addImage() 0 26 3
A getDestinationPath() 0 4 1
A getImageSet() 0 6 2
1
<?php
2
/**
3
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace CloudControl\Cms\storage\storage;
7
8
9
use CloudControl\Cms\images\ImageResizer;
10
use CloudControl\Cms\storage\factories\ImageFactory;
11
12
class ImagesStorage extends AbstractStorage
13
{
14
    protected $imagesDir;
15
16
    /**
17
     * @param \CloudControl\Cms\storage\Repository $repository
18
     * @param string $imagesDir
19
     */
20
    public function __construct($repository, $imagesDir)
21
    {
22
        parent::__construct($repository);
23
        $this->imagesDir = $imagesDir;
24
    }
25
26
27
    /**
28
     * @var ImageSetStorage
29
     */
30
    protected $imageSet;
31
32
    /**
33
     * Get all images
34
     *
35
     * @return array
36
     */
37
    public function getImages()
38
    {
39
        return $this->repository->images;
40
    }
41
42
    /**
43
     * @param $postValues
44
     *
45
     * @return \CloudControl\Cms\storage\entities\Image
46
     * @throws \Exception
47
     */
48
    public function addImage($postValues)
49
    {
50
        $destinationPath = $this->getDestinationPath();
51
52
        $filename = $this->validateFilename($postValues['name'], $destinationPath);
53
        $destination = $destinationPath . DIRECTORY_SEPARATOR . $filename;
54
55
        if ('0' != $postValues['error']) {
56
            throw new \RuntimeException('Error uploading file. Error code: ' . $postValues['error']);
57
        }
58
59
        if (move_uploaded_file($postValues['tmp_name'], $destination)) {
60
            $imageResizer = new ImageResizer($this->getImageSet()->getImageSet());
61
            $fileNames = $imageResizer->applyImageSetToImage($destination);
62
            $fileNames['original'] = $filename;
63
            $imageObject = ImageFactory::createImageFromPostValues($postValues, $filename, $fileNames);
64
65
            $images = $this->repository->images;
66
            $images[] = $imageObject;
67
            $this->repository->images = $images;
68
69
            $this->save();
70
71
            return $imageObject;
72
        } else {
73
            throw new \RuntimeException('Error moving uploaded file');
74
        }
75
    }
76
77
    /**
78
     * Delete image by name
79
     * @param $filename
80
     */
81
    public function deleteImageByName($filename)
82
    {
83
        $destinationPath = $this->getDestinationPath();
84
85
        $images = $this->getImages();
86
87
        foreach ($images as $key => $image) {
88
            if ($image->file == $filename) {
89
                foreach ($image->set as $imageSetFilename) {
90
                    $destination = $destinationPath . '/' . $imageSetFilename;
91
                    if (file_exists($destination)) {
92
                        unlink($destination);
93
                    }
94
                }
95
                unset($images[$key]);
96
            }
97
        }
98
        $images = array_values($images);
99
        $this->repository->images = $images;
100
        $this->save();
101
    }
102
103
    /**
104
     * @param $filename
105
     *
106
     * @return \stdClass|null
107
     */
108
    public function getImageByName($filename)
109
    {
110
        $images = $this->getImages();
111
        foreach ($images as $image) {
112
            if ($image->file == $filename) {
113
                return $image;
114
            }
115
        }
116
117
        return null;
118
    }
119
120
    /**
121
     * @return \CloudControl\Cms\storage\storage\ImageSetStorage
122
     */
123
    private function getImageSet()
124
    {
125
        if (!$this->imageSet instanceof ImageSetStorage) {
0 ignored issues
show
introduced by
$this->imageSet is always a sub-type of CloudControl\Cms\storage\storage\ImageSetStorage.
Loading history...
126
            $this->imageSet = new ImageSetStorage($this->repository);
127
        }
128
        return $this->imageSet;
129
    }
130
131
    /**
132
     * @return bool|string
133
     */
134
    private function getDestinationPath()
135
    {
136
        $destinationPath = realpath($this->imagesDir . DIRECTORY_SEPARATOR);
137
        return $destinationPath;
138
    }
139
}