Completed
Pull Request — master (#72)
by Oliver
02:12
created

NotifynderCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 1
Metric Value
c 5
b 3
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
1
<?php namespace Fenos\Notifynder\Models;
2
3
use Fenos\Notifynder\Contracts\NotifynderTranslator;
4
use Fenos\Notifynder\Exceptions\NotificationTranslationNotFoundException;
5
use Fenos\Notifynder\Parsers\NotifynderParser;
6
use Illuminate\Database\Eloquent\Collection;
7
8
/**
9
 * Class NotifynderCollection
10
 *
11
 * @package Fenos\Notifynder\Models
12
 */
13
class NotifynderCollection extends Collection
14
{
15
    /**
16
     * Domain used for grouping translations
17
     *
18
     * @var string
19
     */
20
    private $translationDomain;
21
22
    /**
23
     * Defines if translation is enabled
24
     * @var boolean
25
     */
26
    private $translate;
27
28
    /**
29
     * @param array $models
30
     */
31
    public function __construct($models)
32
    {
33
        $this->translationDomain = config('notifynder.translate.domain', 'notifynder');
34
        $this->translate = config('notifynder.translate.enabled', false);
35
        parent::__construct($models);
36
    }
37
38
    /**
39
     * Parse the body of the notification
40
     *
41
     * @return $this
42
     */
43
    public function parse()
44
    {
45
        $parser = new NotifynderParser();
46
47
        foreach ($this->items as $key => $item) {
48
49
            if ($this->translate) {
50
                $item = $this->translate($item);
51
            }
52
53
            $this->items[$key]['text'] = $parser->parse($item);
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * This method translate the body text from
61
     * another language. It used by collection
62
     *
63
     * @return Collection
64
     */
65
    private function translate($item)
66
    {
67
        $key = $this->translationDomain . '.' . $item['body']['name'];
68
        $translation = trans($key);
69
70
        $item['body']['text'] = $translation;
71
72
        return $item;
73
    }
74
}