Completed
Push — master ( c1ce98...b3e799 )
by Kirill
03:09
created

AchieveSubscriber::getAchievementInstances()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 Domains\Bot;
12
13
use Domains\Achieve;
14
use Domains\Bot\Achievements\DocsAchieve;
15
use Domains\Bot\Achievements\Karma100Achieve;
16
use Domains\Bot\Achievements\Karma10Achieve;
17
use Domains\Bot\Achievements\Karma500Achieve;
18
use Domains\Bot\Achievements\Karma50Achieve;
19
use Domains\Bot\Achievements\Thanks100Achieve;
20
use Domains\Bot\Achievements\Thanks10Karma0Achieve;
21
use Domains\Bot\Achievements\Thanks20Achieve;
22
use Domains\Bot\Achievements\Thanks50Achieve;
23
use Domains\Room;
24
use Illuminate\Contracts\Support\Arrayable;
25
use Illuminate\Contracts\Support\Jsonable;
26
use Illuminate\Support\Collection;
27
use Interfaces\Gitter\Subscriber\SubscriberInterface;
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
     * Subscribe achievements
70
     */
71
    public function handle()
72
    {
73
        Achieve::created(function (Achieve $achieve) {
74
            $room = \App::make(Room::class);
75
76
            $room->write(
77
                \Lang::get('achieve.receiving', [
78
                    'user'        => $achieve->user->login,
79
                    'title'       => $achieve->title,
80
                    'description' => $achieve->description,
81
                    'image'       => $achieve->image,
82
                ])
83
            );
84
        });
85
    }
86
87
    /**
88
     * @param int $options
89
     * @return string
90
     */
91
    public function toJson($options = 0): string
92
    {
93
        return json_encode($this->toArray(), $options);
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function toArray(): array
100
    {
101
        return $this
102
            ->toCollection()
103
            ->toArray();
104
    }
105
106
    /**
107
     * @return Collection
108
     */
109
    public function toCollection(): Collection
110
    {
111
        return new Collection($this->getAchievementInstances());
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function getAchievementInstances(): array
118
    {
119
        return $this->instances;
120
    }
121
}
122