Passed
Push — master ( 3f6c85...9c6499 )
by Jens
02:40
created

BricksStorage   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 90
loc 90
rs 10
wmc 11
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBricks() 4 4 1
A addBrick() 10 10 1
A getBrickBySlug() 11 11 3
A saveBrick() 13 13 3
A deleteBrickBySlug() 13 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\BrickFactory;
10
11 View Code Duplication
class BricksStorage extends AbstractStorage
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
	/**
14
	 * @return array
15
	 */
16
	public function getBricks()
17
	{
18
		return $this->repository->bricks;
19
	}
20
21
	/**
22
	 * Add a brick
23
	 *
24
	 * @param $postValues
25
	 *
26
	 * @throws \Exception
27
	 */
28
	public function addBrick($postValues)
29
	{
30
		$brickObject = BrickFactory::createBrickFromPostValues($postValues);
31
32
		$bricks = $this->repository->bricks;
33
		$bricks[] = $brickObject;
34
		$this->repository->bricks = $bricks;
35
36
		$this->save();
37
	}
38
39
	/**
40
	 * Get a brick by its slug
41
	 *
42
	 * @param $slug
43
	 *
44
	 * @return \stdClass
45
	 */
46
	public function getBrickBySlug($slug)
47
	{
48
		$bricks = $this->repository->bricks;
49
		foreach ($bricks as $brick) {
50
			if ($brick->slug == $slug) {
51
				return $brick;
52
			}
53
		}
54
55
		return null;
56
	}
57
58
	/**
59
	 * Save changes to a brick
60
	 *
61
	 * @param $slug
62
	 * @param $postValues
63
	 *
64
	 * @throws \Exception
65
	 */
66
	public function saveBrick($slug, $postValues)
67
	{
68
		$brickObject = BrickFactory::createBrickFromPostValues($postValues);
69
70
		$bricks = $this->repository->bricks;
71
		foreach ($bricks as $key => $brick) {
72
			if ($brick->slug == $slug) {
73
				$bricks[$key] = $brickObject;
74
			}
75
		}
76
		$this->repository->bricks = $bricks;
77
		$this->save();
78
	}
79
80
	/**
81
	 * Delete a brick by its slug
82
	 *
83
	 * @param $slug
84
	 *
85
	 * @throws \Exception
86
	 */
87
	public function deleteBrickBySlug($slug)
88
	{
89
		$bricks = $this->repository->bricks;
90
		foreach ($bricks as $key => $brickObject) {
91
			if ($brickObject->slug == $slug) {
92
				unset($bricks[$key]);
93
			}
94
		}
95
96
		$bricks = array_values($bricks);
97
		$this->repository->bricks = $bricks;
98
		$this->save();
99
	}
100
}