Completed
Push — master ( 9abbc7...a687e5 )
by Fabrizio
03:03
created

NotifynderCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 3 Features 1
Metric Value
wmc 7
c 5
b 3
f 1
lcom 1
cbo 3
dl 0
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 11 2
A __construct() 0 4 1
A translator() 0 4 1
A translate() 0 18 3
1
<?php
2
3
namespace Fenos\Notifynder\Models;
4
5
use Fenos\Notifynder\Contracts\NotifynderTranslator;
6
use Fenos\Notifynder\Exceptions\NotificationTranslationNotFoundException;
7
use Fenos\Notifynder\Parsers\NotifynderParser;
8
use Illuminate\Database\Eloquent\Collection;
9
10
/**
11
 * Class NotifynderCollection.
12
 */
13
class NotifynderCollection extends Collection
14
{
15
    /**
16
     * @var NotifynderTranslator
17
     */
18
    protected $notifynderTranslator;
19
20
    /**
21
     * @param array $models
22
     */
23
    public function __construct($models)
24
    {
25
        parent::__construct($models);
26
    }
27
28
    /**
29
     * Translator instance.
30
     *
31
     * @return NotifynderTranslator
32
     */
33
    public function translator()
34
    {
35
        return app('notifynder.translator');
36
    }
37
38
    /**
39
     * This method translate the body text from
40
     * another language. It used by collection.
41
     *
42
     * @param $language (String)
43
     * @return Collection
44
     */
45
    public function translate($language)
46
    {
47
        // Loop through the notifications
48
        foreach ($this->items as $key => $item) {
49
            try {
50
                $translation = $this->translator()
51
                    ->translate($language, $this->items[$key]['body']['name']);
52
53
                $this->items[$key]['body']['text'] = $translation;
54
            } catch (NotificationTranslationNotFoundException $e) {
55
                $this->items[$key]['body']['text'];
56
            }
57
        }
58
59
        $this->parse();
60
61
        return $this;
62
    }
63
64
    /**
65
     * Parse the body of the notification.
66
     *
67
     * @return $this
68
     */
69
    public function parse()
70
    {
71
        $parser = new NotifynderParser();
72
73
        foreach ($this->items as $key => $item) {
74
            $this->items[$key]['notify_body'] = $parser->parse($item);
75
            $this->items[$key]['text'] = $this->items[$key]['notify_body'];
76
        }
77
78
        return $this;
79
    }
80
}
81