Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

AchieveSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3
Metric Value
wmc 7
lcom 2
cbo 3
dl 0
loc 90
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getAchievementInstances() 0 4 1
A handle() 0 15 1
A toCollection() 0 4 1
A toArray() 0 6 1
A toJson() 0 4 1
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 11.10.2015 8:30
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Core\Subscribers;
12
13
use Domains\Room;
14
use Domains\Achieve;
15
use Illuminate\Support\Collection;
16
use Illuminate\Contracts\Support\Jsonable;
17
use Illuminate\Contracts\Support\Arrayable;
18
use Interfaces\Gitter\Subscriber\SubscriberInterface;
19
use Core\Subscribers\Achievements\Karma10Achieve;
20
use Core\Subscribers\Achievements\Karma50Achieve;
21
use Core\Subscribers\Achievements\Karma100Achieve;
22
use Core\Subscribers\Achievements\Karma500Achieve;
23
use Core\Subscribers\Achievements\Thanks20Achieve;
24
use Core\Subscribers\Achievements\Thanks50Achieve;
25
use Core\Subscribers\Achievements\Thanks100Achieve;
26
use Core\Subscribers\Achievements\Thanks10Karma0Achieve;
27
use Core\Subscribers\Achievements\DocsAchieve;
28
29
/**
30
 * Class AchieveSubscriber
31
 */
32
class AchieveSubscriber implements
33
    SubscriberInterface,
34
    Arrayable,
35
    Jsonable
36
{
37
    /**
38
     * @var array
39
     */
40
    protected $achievements = [
41
        Karma10Achieve::class,
42
        Karma50Achieve::class,
43
        Karma100Achieve::class,
44
        Karma500Achieve::class,
45
        Thanks20Achieve::class,
46
        Thanks50Achieve::class,
47
        Thanks100Achieve::class,
48
        Thanks10Karma0Achieve::class,
49
        DocsAchieve::class
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    protected $instances = [];
56
57
    /**
58
     * AchieveSubscriber constructor.
59
     */
60
    public function __construct()
61
    {
62
        foreach ($this->achievements as $achieve) {
63
            $this->instances[] = ($instance = new $achieve);
64
            $instance->handle();
65
        }
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getAchievementInstances(): array
72
    {
73
        return $this->instances;
74
    }
75
76
    /**
77
     * Subscribe achievements
78
     */
79
    public function handle()
80
    {
81
        Achieve::created(function (Achieve $achieve) {
82
            $room = \App::make(Room::class);
83
84
            $room->write(
85
                \Lang::get('achieve.receiving', [
86
                    'user'        => $achieve->user->login,
87
                    'title'       => $achieve->title,
88
                    'description' => $achieve->description,
89
                    'image'       => $achieve->image,
90
                ])
91
            );
92
        });
93
    }
94
95
    /**
96
     * @return Collection
97
     */
98
    public function toCollection(): Collection
99
    {
100
        return new Collection($this->getAchievementInstances());
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function toArray(): array
107
    {
108
        return $this
109
            ->toCollection()
110
            ->toArray();
111
    }
112
113
    /**
114
     * @param int $options
115
     * @return string
116
     */
117
    public function toJson($options = 0): string
118
    {
119
        return json_encode($this->toArray(), $options);
120
    }
121
}