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

ValuelistsStorage   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
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 82
loc 82
rs 10
wmc 11
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getValuelists() 4 4 1
A addValuelist() 8 8 1
A saveValuelist() 13 13 3
A getValuelistBySlug() 11 11 3
A deleteValuelistBySlug() 12 12 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
 * User: Jens
4
 * Date: 8-6-2017
5
 * Time: 14:18
6
 */
7
8
namespace library\storage\storage;
9
10
11
use library\storage\factories\ValuelistFactory;
12
13 View Code Duplication
class ValuelistsStorage 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...
14
{
15
    /**
16
     * Get all valuelists
17
     *
18
     * @return mixed
19
     */
20
    public function getValuelists()
21
    {
22
        return $this->repository->valuelists;
23
    }
24
25
    public function addValuelist($postValues)
26
    {
27
        $valueListObject = ValuelistFactory::createValuelistFromPostValues($postValues);
28
        $valuelists = $this->repository->valuelists;
29
        $valuelists[] = $valueListObject;
30
        $this->repository->valuelists = $valuelists;
31
        $this->save();
32
    }
33
34
    /**
35
     * Save changes to a valuelist
36
     *
37
     * @param $slug
38
     * @param $postValues
39
     *
40
     * @throws \Exception
41
     */
42
    public function saveValuelist($slug, $postValues)
43
    {
44
        $valuelistObject = ValuelistFactory::createValuelistFromPostValues($postValues);
45
46
        $valuelists = $this->repository->valuelists;
47
        foreach ($valuelists as $key => $valuelist) {
48
            if ($valuelist->slug == $slug) {
49
                $valuelists[$key] = $valuelistObject;
50
            }
51
        }
52
        $this->repository->valuelists = $valuelists;
53
        $this->save();
54
    }
55
56
    /**
57
     * Get a valuelist by its slug
58
     *
59
     * @param $slug
60
     *
61
     * @return mixed
62
     */
63
    public function getValuelistBySlug($slug)
64
    {
65
        $valuelists = $this->repository->valuelists;
66
        foreach ($valuelists as $valuelist) {
67
            if ($valuelist->slug == $slug) {
68
                return $valuelist;
69
            }
70
        }
71
72
        return null;
73
    }
74
75
    /**
76
     * Delete a sitemap item by its slug
77
     *
78
     * @param $slug
79
     *
80
     * @throws \Exception
81
     */
82
    public function deleteValuelistBySlug($slug)
83
    {
84
        $valuelists = $this->repository->valuelists;
85
        foreach ($valuelists as $key => $valuelist) {
86
            if ($valuelist->slug == $slug) {
87
                unset($valuelists[$key]);
88
            }
89
        }
90
        $valuelists = array_values($valuelists);
91
        $this->repository->valuelists = $valuelists;
92
        $this->save();
93
    }
94
}