AbstractAchieve   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 10 2
A __set() 0 4 1
A toArray() 0 9 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 6:09
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 Interfaces\Gitter\Achieve;
12
13
use Domains\User;
14
use Domains\Achieve;
15
use Carbon\Carbon;
16
use Illuminate\Contracts\Support\Jsonable;
17
use Illuminate\Contracts\Support\Arrayable;
18
19
/**
20
 * Class AbstractAchieve
21
 */
22
abstract class AbstractAchieve implements
23
    AchieveInterface,
24
    Arrayable,
25
    Jsonable
26
{
27
    /**
28
     * Achieve title
29
     * @var string
30
     */
31
    public $title = 'undefined';
32
33
    /**
34
     * Achieve description
35
     * @var string
36
     */
37
    public $description = 'undefined';
38
39
    /**
40
     * Achieve image link
41
     * @var string
42
     */
43
    public $image = '/img/achievements/karma-10.gif';
44
45
    /**
46
     * @var null
47
     */
48
    public $name = null;
49
50
    /**
51
     * @var array
52
     */
53
    protected $properties = [];
54
55
    /**
56
     * @constructor
57
     */
58
    public function __construct()
59
    {
60
        $this->name = basename(static::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like basename(static::class) of type string is incompatible with the declared type null of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
    }
62
63
    /**
64
     * @param User $user
65
     * @param Carbon|null $createdAt
66
     * @return Achieve
67
     * @throws \LogicException
68
     */
69
    public function create(User $user, Carbon $createdAt = null): Achieve
70
    {
71
        $achieve = Achieve::create([
72
            'name'       => $this->name,
73
            'user_id'    => $user->id,
74
            'created_at' => $createdAt ?: Carbon::now(),
75
        ]);
76
77
        return $achieve;
78
    }
79
80
    /**
81
     * @param $key
82
     * @param $value
83
     */
84
    public function __set($key, $value)
85
    {
86
        $this->properties[$key] = $value;
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function toArray(): array
93
    {
94
        return array_merge([
95
            'name'        => $this->name,
96
            'title'       => $this->title,
97
            'description' => $this->description,
98
            'image'       => $this->image,
99
        ], $this->properties);
100
    }
101
102
    /**
103
     * @param int $options
104
     * @return string
105
     */
106
    public function toJson($options = 0): string
107
    {
108
        return json_encode($this->toArray(), $options);
109
    }
110
}
111