Passed
Push — develop ( 39fefa...eeeda6 )
by Jens
03:13
created

ImageSetStorage::getImageSet()   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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace library\storage\storage;
7
8
9
use library\storage\factories\ImageSetFactory;
10
11
class ImageSetStorage extends Storage
12
{
13
14
	/**
15
	 * @return mixed
16
	 */
17
	public function getImageSet()
18
	{
19
		return $this->repository->imageSet;
20
	}
21
22
	/**
23
	 * Get Image by slug
24
	 *
25
	 * @param $slug
26
	 *
27
	 * @return \stdClass
28
	 */
29
	public function getImageSetBySlug($slug)
30
	{
31
		$imageSet = $this->getImageSet();
32
		foreach ($imageSet as $set) {
33
			if ($set->slug == $slug) {
34
				return $set;
35
			}
36
		}
37
38
		return null;
39
	}
40
41
	/**
42
	 * Add image set
43
	 *
44
	 * @param $postValues
45
	 *
46
	 * @throws \Exception
47
	 */
48
	public function addImageSet($postValues)
49
	{
50
		$imageSetObject = ImageSetFactory::createImageSetFromPostValues($postValues);
51
52
		$imageSet = $this->repository->imageSet;
53
		$imageSet[] = $imageSetObject;
54
		$this->repository->imageSet = $imageSet;
55
56
		$this->save();
57
	}
58
59
	/**
60
	 * Save Image Set by it's slug
61
	 *
62
	 * @param $slug
63
	 * @param $postValues
64
	 *
65
	 * @throws \Exception
66
	 */
67
	public function saveImageSet($slug, $postValues)
68
	{
69
		$imageSetObject = ImageSetFactory::createImageSetFromPostValues($postValues);
70
71
		$imageSet = $this->repository->imageSet;
72
		foreach ($imageSet as $key => $set) {
73
			if ($set->slug == $slug) {
74
				$imageSet[$key] = $imageSetObject;
75
			}
76
		}
77
		$this->repository->imageSet = $imageSet;
78
		$this->save();
79
	}
80
81
	/**
82
	 * Delete Image Set by its slug
83
	 *
84
	 * @param $slug
85
	 *
86
	 * @throws \Exception
87
	 */
88
	public function deleteImageSetBySlug($slug)
89
	{
90
		$imageSet = $this->getImageSet();
91
92
		foreach ($imageSet as $key => $set) {
93
			if ($set->slug == $slug) {
94
				unset($imageSet[$key]);
95
			}
96
		}
97
		$imageSet = array_values($imageSet);
98
		$this->repository->imageSet = $imageSet;
99
		$this->save();
100
	}
101
102
	/**
103
	 * Get the image set with the smallest size
104
	 *
105
	 * @return \stdClass
106
	 */
107
	public function getSmallestImageSet()
108
	{
109
		$imageSet = $this->getImageSet();
110
111
		$returnSize = PHP_INT_MAX;
112
		$returnSet = null;
113
114
		foreach ($imageSet as $set) {
115
			$size = $set->width * $set->height;
116
			if ($size < $returnSize) {
117
				$returnSize = $size;
118
				$returnSet = $set;
119
			}
120
		}
121
122
		if ($returnSet === null) {
123
			$returnSet = new \stdClass();
124
			$returnSet->slug = 'original';
125
		}
126
127
		return $returnSet;
128
	}
129
}