Test Failed
Push — master ( dc68d1...1f5199 )
by Mathieu
02:31
created

NewsLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 21.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 26
loc 123
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 8 8 1
A newsProto() 0 4 1
A published() 9 9 1
A expired() 9 9 1
A upcoming() 0 6 1
A archive() 0 6 1
A median() 0 4 1
A objType() 0 4 1
A setMedian() 0 6 1
A setObjType() 0 6 1

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
namespace Charcoal\Cms\Service\Loader;
4
5
// dependencies from `charcoal-core`
6
use Charcoal\Loader\CollectionLoader;
7
8
// PHP dependencies
9
use DateTime;
10
11
/**
12
 * News Loader
13
 */
14
class NewsLoader extends AbstractLoader
15
{
16
    /**
17
     * @var string $median The median between upcoming and archive entries.
18
     */
19
    protected $median;
20
21
    /**
22
     * @var object $objType The object to load.
23
     */
24
    protected $objType;
25
26
    /**
27
     * @return CollectionLoader
28
     */
29 View Code Duplication
    public function all()
0 ignored issues
show
Duplication introduced by
This method 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...
30
    {
31
        $proto = $this->newsProto();
32
        $loader = $this->collectionLoader()->setModel($proto);
33
        $loader->addFilter('active', true);
34
35
        return $loader;
36
    }
37
38
    public function newsProto()
39
    {
40
        return $this->modelFactory()->get($this->objType());
41
    }
42
43
    /**
44
     * @return CollectionLoader
45
     */
46 View Code Duplication
    public function published()
0 ignored issues
show
Duplication introduced by
This method 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...
47
    {
48
        $now = new DateTime();
49
        $loader = $this->all();
50
        $loader->addFilter('publish_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '<=' ])
51
            ->addFilter('expiry_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '>=' ]);
52
53
        return $loader;
54
    }
55
56
    /**
57
     * @return CollectionLoader
58
     */
59 View Code Duplication
    public function expired()
0 ignored issues
show
Duplication introduced by
This method 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...
60
    {
61
        $now = new DateTime();
62
        $loader = $this->all();
63
        $loader->addFilter('publish_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '<=' ])
64
            ->addFilter('expiry_date', $now->format('Y-m-d H:i:s'), [ 'operator' => '<=' ]);
65
66
        return $loader;
67
    }
68
69
    /**
70
     * Fetch upcoming entries based on the median or now.
71
     * @return CollectionLoader
72
     */
73
    public function upcoming()
74
    {
75
        $loader = $this->published();
76
77
        return $loader;
78
    }
79
80
    /**
81
     * Fetch upcoming entries based on the median or now.
82
     * @return CollectionLoader
83
     */
84
    public function archive()
85
    {
86
        $loader = $this->expired();
87
88
        return $loader;
89
    }
90
91
    // ==========================================================================
92
    // GETTERS
93
    // ==========================================================================
94
95
    /**
96
     * @return mixed
97
     */
98
    public function median()
99
    {
100
        return $this->median;
101
    }
102
103
    /**
104
     * @return object
105
     */
106
    public function objType()
107
    {
108
        return $this->objType;
109
    }
110
111
    // ==========================================================================
112
    // SETTERS
113
    // ==========================================================================
114
115
    /**
116
     * @param string $median The median between upcoming and archive.
117
     * @return self
118
     */
119
    public function setMedian($median)
120
    {
121
        $this->median = $median;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param object $objType The object type.
128
     * @return self
129
     */
130
    public function setObjType($objType)
131
    {
132
        $this->objType = $objType;
133
134
        return $this;
135
    }
136
}
137