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

NewsManager   D

Complexity

Total Complexity 81

Size/Duplication

Total Lines 593
Duplicated Lines 41.15 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 81
lcom 1
cbo 6
dl 244
loc 593
rs 4.8717
c 0
b 0
f 0

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 19 19 2
B entries() 23 23 5
A entry() 13 13 4
A all() 0 10 2
A loadCategoryItems() 8 8 1
A categoryItem() 0 10 2
C featList() 56 56 14
B archive() 25 25 5
A latest() 11 11 2
A prev() 0 8 2
A next() 0 8 2
C currentPage() 23 23 7
A currentNews() 0 8 2
A numPerPage() 0 4 1
A entryCycle() 0 4 1
A numNews() 0 4 1
A numPages() 17 17 3
A hasPager() 0 4 1
A page() 0 4 1
A category() 0 4 1
A objType() 0 4 1
A featIdent() 0 4 1
A loader() 0 4 1
A setCurrentNews() 0 6 1
A setNumPerPage() 0 6 1
A setEntryCycle() 0 6 1
A setPage() 0 6 1
A setCategory() 0 6 1
A setObjType() 0 6 1
A setFeatIdent() 0 6 1
A setLoader() 0 6 1
C setPrevNext() 49 49 11

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like NewsManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use NewsManager, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Charcoal\Cms\Service\Manager;
4
5
// dependencies from `charcoal-core`
6
use Charcoal\Model\Collection;
7
8
// dependencies from `charcoal-base`
9
use Charcoal\Object\CategoryInterface;
10
use Charcoal\Object\CategoryTrait;
11
12
// Local dependencies
13
use Charcoal\Cms\Config\CmsConfig;
14
use Charcoal\Cms\NewsInterface;
15
use Charcoal\Cms\Service\Loader\NewsLoader;
16
17
// Psr-7 dependencies
18
use Exception;
19
20
/**
21
 * News manager
22
 */
23
class NewsManager extends AbstractManager
24
{
25
    use CategoryTrait;
26
27
    /** @var NewsInterface $currentNews The current news. */
28
    protected $currentNews;
29
30
    /** @var integer $currentPage The current Page. */
31
    protected $currentPage;
32
33
    /** @var integer $numPerPage News by page. */
34
    protected $numPerPage = 0;
35
36
    /** @var integer $numPage How many pages. */
37
    protected $numPage;
38
39
    /** @var boolean $pageCycle Does the pager can cycle indefinitely. */
40
    protected $entryCycle = false;
41
42
    /** @var NewsInterface $nextNews */
43
    protected $nextNews;
44
45
    /** @var NewsInterface $prevNews */
46
    protected $prevNews;
47
48
    /** @var integer $page */
49
    protected $page = 0;
50
51
    /** @var integer $category */
52
    protected $category = 0;
53
54
    /** @var NewsInterface[] $all All the news. */
55
    protected $all = [];
56
57
    /** @var NewsInterface[] $entries The news collection. */
58
    protected $entries = [];
59
60
    /** @var NewsInterface[] $archive The archive news collection. */
61
    protected $archive = [];
62
63
    /** @var NewsInterface $entry A news. */
64
    protected $entry;
65
66
    /** @var object $objType The news object model. */
67
    protected $objType;
68
69
    /** @var string $featIdent The config ident for featured news. */
70
    protected $featIdent;
71
72
    /** @var NewsInterface[] $featList The config ident for featured news. */
73
    protected $featList = [];
74
75
    /** @var NewsLoader $loader The news loader provider. */
76
    protected $loader;
77
78
    /**
79
     * @var array
80
     */
81
    protected $categoryItem = [];
82
83
    /**
84
     * NewsManager constructor.
85
     * @param array $data The Data.
86
     * @throws Exception When $data index is not set.
87
     */
88 View Code Duplication
    public function __construct(array $data)
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...
89
    {
90
        parent::__construct($data);
91
92
        if (!isset($data['news/loader'])) {
93
            throw new Exception('News Loader must be defined in the NewsManager constructor.');
94
        }
95
96
        $this->setLoader($data['news/loader']);
97
98
        /** @var CmsConfig $newsConfig */
99
        $newsConfig = $this->adminConfig()->newsConfig();
100
101
        $this->setNumPerPage($newsConfig->get('numPerPage'));
102
        $this->setEntryCycle($newsConfig->get('entryCycle'));
103
        $this->setObjType($newsConfig->get('objType'));
104
        $this->setCategoryItemType($newsConfig->get('category'));
105
        $this->setFeatIdent($newsConfig->get('configFeatIdent'));
106
    }
107
108
    /**
109
     * To be displayed news list.
110
     * @return mixed The news collection.
111
     */
112 View Code Duplication
    public function entries()
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...
113
    {
114
        $page = $this->page();
115
        $cat = $this->category();
116
        if (isset($this->entries[$cat])) {
117
            if (isset($this->entries[$cat][$page])) {
118
                return $this->entries[$cat][$page];
119
            }
120
        }
121
        $loader = $this->loader()->upcoming();
122
        $loader->addOrder('news_date', 'desc');
123
124
        if ($this->category()) {
125
            $loader->addFilter('category', $this->category(), [ 'operator' => 'in' ]);
126
        }
127
        if ($this->numPerPage()) {
128
            $loader->setPage($this->page());
129
            $loader->setNumPerPage($this->numPerPage());
130
        }
131
        $this->entries[$cat][$page] = $loader->load();
132
133
        return $this->entries[$cat][$page];
134
    }
135
136
    /**
137
     * @param integer|null $id The news id.
138
     * @return mixed
139
     */
140 View Code Duplication
    public function entry($id = null)
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...
141
    {
142
        if (!$id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
143
            return $this->currentNews();
144
        }
145
146
        if (!isset($this->entry[$id])) {
147
            $entry = $this->modelFactory()->get($this->objType())->loadFrom('id', $id);
148
            $this->entry[$id] = $entry->id() ? $entry : $this->currentNews();
149
        }
150
151
        return $this->entry[$id];
152
    }
153
154
    /**
155
     * All available news.
156
     * @return NewsInterface[]|Collection The news collection.
157
     */
158
    public function all()
159
    {
160
        if ($this->all) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->all of type Charcoal\Cms\NewsInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
161
            return $this->all;
162
        }
163
164
        $this->all = $this->loader()->all()->addOrder('news_date', 'desc')->load();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->loader()->all()->..._date', 'desc')->load() can also be of type object. However, the property $all is declared as type array<integer,object<Charcoal\Cms\NewsInterface>>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
165
166
        return $this->all;
167
    }
168
169
    /**
170
     * @return CategoryInterface[]|Collection The category collection.
171
     */
172 View Code Duplication
    public function loadCategoryItems()
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...
173
    {
174
        $proto = $this->modelFactory()->create($this->categoryItemType());
175
        $loader = $this->collectionLoader()->setModel($proto);
176
        $loader->addFilter('active', true);
177
178
        return $loader->load();
179
    }
180
181
    /**
182
     * @param integer $id The category id.
183
     * @return CategoryInterface
184
     */
185
    public function categoryItem($id)
186
    {
187
        if (isset($this->categoryItem[$id])) {
188
            return $this->categoryItem[$id];
189
        }
190
        $category = $this->modelFactory()->create($this->categoryItemType());
191
192
        $this->categoryItem[$id] = $category->load($id);
193
        return $this->categoryItem[$id];
194
    }
195
196
    /**
197
     * @return mixed
198
     * @param array $options The options for the collection loader.
199
     * @throws Exception When featured news ident is not valid.
200
     */
201 View Code Duplication
    public function featList(array $options = [])
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...
202
    {
203
        if ($this->featList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->featList of type Charcoal\Cms\NewsInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
204
            return $this->featList;
205
        }
206
207
        $loader = $this->loader()->published();
208
        $ident = $this->featIdent();
209
        $config = $this->adminConfig();
210
211
        if (!$ident || !method_exists($config, $ident)) {
212
            throw new Exception(sprintf(
213
                'The featured news ident "%s" doesn\'t exist the class "%s"',
214
                $ident,
215
                get_class($config)
216
            ));
217
        }
218
        $ids = $config->{$ident}();
219
220
        if (!$ids) {
221
            return null;
222
        }
223
224
        $ids = explode(',', $ids);
225
226
        $loader->addFilter('id', $ids, [ 'operator' => 'in' ])
227
            ->addOrder('id', 'values', [ 'values' => $ids ]);
228
229
        if (count($options) > 0) {
230
            foreach ($options as $key => $option) {
231
                switch ($key) {
232
                    case 'filters':
233
                        $filters = $option;
234
                        foreach ($filters as $f) {
235
                            $filter[] = $f['property'] ?: '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$filter was never initialized. Although not strictly required by PHP, it is generally a good practice to add $filter = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
236
                            $filter[] = $f['val'] ?: '';
0 ignored issues
show
Bug introduced by
The variable $filter does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
237
                            $filter[] = $f['options'] ?: '';
238
                            $filter = join(',', $filter);
239
240
                            $loader->addFilter($filter);
241
                        }
242
                        break;
243
                    case 'page':
244
                        $loader->setPage($option);
245
                        break;
246
                    case 'numPerPage':
247
                        $loader->setNumPerPage($option);
248
                        break;
249
                }
250
            }
251
        }
252
253
        $this->featList = $loader->load();
0 ignored issues
show
Documentation Bug introduced by
It seems like $loader->load() can also be of type object. However, the property $featList is declared as type array<integer,object<Charcoal\Cms\NewsInterface>>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
254
255
        return $this->featList;
256
    }
257
258
    /**
259
     * @return NewsInterface[]|Collection
260
     */
261 View Code Duplication
    public function archive()
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...
262
    {
263
        $page = $this->page();
264
        $cat = $this->category();
265
        if (isset($this->archive[$cat])) {
266
            if (isset($this->archive[$cat][$page])) {
267
                return $this->archive[$cat][$page];
268
            }
269
        }
270
271
        $loader = $this->loader()->archive();
272
        $loader->addOrder('news_date', 'desc');
273
274
        if ($this->category()) {
275
            $loader->addFilter('category', $this->category(), [ 'operator' => 'in' ]);
276
        }
277
        if ($this->numPerPage()) {
278
            $loader->setPage($this->page());
279
            $loader->setNumPerPage($this->numPerPage());
280
        }
281
282
        $this->archive[$cat][$page] = $loader->load();
283
284
        return $this->archive[$cat][$page];
285
    }
286
287
    /**
288
     * Get the latest news.
289
     * @return NewsInterface|array The latest news.
290
     */
291 View Code Duplication
    public function latest()
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...
292
    {
293
        $entries = $this->entries();
294
295
        if (isset($entries[0])) {
296
            return $entries[0];
297
        } else {
298
            // NO NEWS!
299
            return [];
300
        }
301
    }
302
303
    /**
304
     * @return mixed The previous news
305
     */
306
    public function prev()
307
    {
308
        if ($this->prevNews) {
309
            return $this->prevNews;
310
        }
311
312
        return $this->setPrevNext()->prevNews;
313
    }
314
315
    /**
316
     * @return mixed The next news
317
     */
318
    public function next()
319
    {
320
        if ($this->nextNews) {
321
            return $this->nextNews;
322
        }
323
324
        return $this->setPrevNext()->nextNews;
325
    }
326
327
    /**
328
     * @return float|int The current news index page ident.
329
     */
330 View Code Duplication
    public function currentPage()
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...
331
    {
332
        if ($this->currentPage) {
333
            return $this->currentPage;
334
        }
335
        if (!$this->currentNews() || !$this->currentNews()['id']) {
336
            $this->currentPage = 1;
337
338
            return 1;
339
        }
340
        $all = $this->all();
341
        $i = 0;
342
        foreach ($all as $news) {
343
            $i++;
344
            if ($news->id() == $this->currentNews()['id']) {
345
                break;
346
            }
347
        }
348
349
        $this->currentPage = $this->numPerPage() ? ceil($i / $this->numPerPage()) : 1;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->numPerPage() ? ce...this->numPerPage()) : 1 can also be of type double. However, the property $currentPage is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
350
351
        return $this->currentPage;
352
    }
353
354
    // ==========================================================================
355
    // GETTERS
356
    // ==========================================================================
357
358
    /**
359
     * @return mixed
360
     */
361
    public function currentNews()
362
    {
363
        if (!$this->currentNews) {
364
            $this->currentNews = $this->latest();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->latest() can also be of type array. However, the property $currentNews is declared as type object<Charcoal\Cms\NewsInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
365
        }
366
367
        return $this->currentNews;
368
    }
369
370
    /**
371
     * @return integer
372
     */
373
    public function numPerPage()
374
    {
375
        return $this->numPerPage;
376
    }
377
378
    /**
379
     * @return boolean
380
     */
381
    public function entryCycle()
382
    {
383
        return $this->entryCycle;
384
    }
385
386
    /**
387
     * Amount of news (total)
388
     * @return integer How many news?
389
     */
390
    public function numNews()
391
    {
392
        return !!(count($this->entries()));
393
    }
394
395
    /**
396
     * The total amount of pages.
397
     * @return float
398
     */
399 View Code Duplication
    public function numPages()
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...
400
    {
401
        if ($this->numPage) {
402
            $this->numPage;
403
        };
404
405
        $entries = $this->entries();
406
        $count = count($entries);
407
408
        if ($this->numPerPage()) {
409
            $this->numPage = ceil($count / $this->numPerPage());
0 ignored issues
show
Documentation Bug introduced by
The property $numPage was declared of type integer, but ceil($count / $this->numPerPage()) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
410
        } else {
411
            $this->numPage = 1;
412
        }
413
414
        return $this->numPage;
415
    }
416
417
    /**
418
     * Is there a pager.
419
     * @return boolean
420
     */
421
    public function hasPager()
422
    {
423
        return ($this->numPages() > 1);
424
    }
425
426
    /**
427
     * @return integer
428
     */
429
    public function page()
430
    {
431
        return $this->page;
432
    }
433
434
    /**
435
     * @return integer
436
     */
437
    public function category()
438
    {
439
        return $this->category;
440
    }
441
442
    /**
443
     * @return mixed
444
     */
445
    public function objType()
446
    {
447
        return $this->objType;
448
    }
449
450
    /**
451
     * @return mixed
452
     */
453
    public function featIdent()
454
    {
455
        return $this->featIdent;
456
    }
457
458
    /**
459
     * @return NewsLoader
460
     */
461
    public function loader()
462
    {
463
        return $this->loader;
464
    }
465
466
    // ==========================================================================
467
    // SETTERS
468
    // ==========================================================================
469
470
    /**
471
     * @param mixed $currentNews The current news context.
472
     * @return self .
473
     */
474
    public function setCurrentNews($currentNews)
475
    {
476
        $this->currentNews = $currentNews;
477
478
        return $this;
479
    }
480
481
    /**
482
     * @param integer $numPerPage The number of news per page.
483
     * @return self
484
     */
485
    public function setNumPerPage($numPerPage)
486
    {
487
        $this->numPerPage = $numPerPage;
488
489
        return $this;
490
    }
491
492
    /**
493
     * @param boolean $entryCycle Next and Prev cycles indefinitely.
494
     * @return self
495
     */
496
    public function setEntryCycle($entryCycle)
497
    {
498
        $this->entryCycle = $entryCycle;
499
500
        return $this;
501
    }
502
503
    /**
504
     * @param integer $page The page number to load.
505
     * @return self
506
     */
507
    public function setPage($page)
508
    {
509
        $this->page = $page;
510
511
        return $this;
512
    }
513
514
    /**
515
     * @param integer $category The current news category.
516
     * @return self
517
     */
518
    public function setCategory($category)
519
    {
520
        $this->category = $category;
521
522
        return $this;
523
    }
524
525
    /**
526
     * @param mixed $objType The object type.
527
     * @return self
528
     */
529
    public function setObjType($objType)
530
    {
531
        $this->objType = $objType;
532
533
        return $this;
534
    }
535
536
    /**
537
     * @param mixed $featIdent The featured list ident.
538
     * @return self
539
     */
540
    public function setFeatIdent($featIdent)
541
    {
542
        $this->featIdent = $featIdent;
543
544
        return $this;
545
    }
546
547
    /**
548
     * @param NewsLoader $loader The news loader provider.
549
     * @return self
550
     */
551
    public function setLoader(NewsLoader $loader)
552
    {
553
        $this->loader = $loader;
554
555
        return $this;
556
    }
557
558
    // ==========================================================================
559
    // UTILS
560
    // ==========================================================================
561
562
    /**
563
     * Set the Prev and Next news
564
     * @return $this
565
     */
566 View Code Duplication
    public function setPrevNext()
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...
567
    {
568
        if ($this->nextNews && $this->prevNews) {
569
            return $this;
570
        }
571
        $entries = $this->entries();
572
573
        $isPrev = false;
574
        $isNext = false;
575
        $firstNews = false;
576
        $lastNews = false;
577
578
        /** @var NewsInterface $news */
579
        foreach ($entries as $news) {
580
            // Obtain th first news.
581
            if (!$firstNews) {
582
                $firstNews = $news;
583
            }
584
            $lastNews = $news;
585
            // Find the current news
586
            if ($news->id() == $this->currentNews()['id']) {
587
                $isNext = true;
588
                $isPrev = true;
589
590
                continue;
591
            }
592
            if (!$isPrev) {
593
                $this->prevNews = $news;
594
            }
595
            // Store the next news
596
            if ($isNext) {
597
                $this->nextNews = $news;
598
599
                $isNext = false;
600
            }
601
        }
602
603
        if ($this->entryCycle()) {
604
            if (!$this->nextNews) {
605
                $this->nextNews = $firstNews;
606
            }
607
608
            if (!$this->prevNews) {
609
                $this->prevNews = $lastNews;
610
            }
611
        }
612
613
        return $this;
614
    }
615
}
616