NewsletterListCollection   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromConfig() 0 12 2
A findByName() 0 14 4
A getDefault() 0 10 3
1
<?php
2
3
namespace Spatie\Newsletter;
4
5
use Illuminate\Support\Collection;
6
use Spatie\Newsletter\Exceptions\InvalidNewsletterList;
7
8
class NewsletterListCollection extends Collection
9
{
10
    /** @var string */
11
    public $defaultListName = '';
12
13
    public static function createFromConfig(array $config): self
14
    {
15
        $collection = new static();
16
17
        foreach ($config['lists'] as $name => $listProperties) {
18
            $collection->push(new NewsletterList($name, $listProperties));
19
        }
20
21
        $collection->defaultListName = $config['defaultListName'];
22
23
        return $collection;
24
    }
25
26
    public function findByName(string $name): NewsletterList
27
    {
28
        if ($name === '') {
29
            return $this->getDefault();
30
        }
31
32
        foreach ($this->items as $newsletterList) {
33
            if ($newsletterList->getName() === $name) {
34
                return $newsletterList;
35
            }
36
        }
37
38
        throw InvalidNewsletterList::noListWithName($name);
39
    }
40
41
    public function getDefault(): NewsletterList
42
    {
43
        foreach ($this->items as $newsletterList) {
44
            if ($newsletterList->getName() === $this->defaultListName) {
45
                return $newsletterList;
46
            }
47
        }
48
49
        throw InvalidNewsletterList::defaultListDoesNotExist($this->defaultListName);
50
    }
51
}
52