Completed
Push — issue-37 ( 681eed...73509a )
by Fèvre
03:17
created

ExperienceSubscriber::subscribe()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace Xetaravel\Listeners\Subscribers;
3
4
use Carbon\Carbon;
5
use Xetaravel\Events\Discuss\PostWasCreatedEvent;
6
use Xetaravel\Models\UserExperience;
7
use Xetaravel\Models\User;
8
9
class ExperienceSubscriber
10
{
11
    /**
12
     * The events mapping to the listener function.
13
     *
14
     * @var array
15
     */
16
    protected $events = [
17
        PostWasCreatedEvent::class => 'postWasCreated',
18
    ];
19
20
    /**
21
     * Register the listeners for the subscriber.
22
     *
23
     * @param Illuminate\Events\Dispatcher $events
24
     *
25
     * @return void
26
     */
27
    public function subscribe($events)
28
    {
29
        foreach ($this->events as $event => $action) {
30
            $events->listen($event, ExperienceSubscriber::class . '@' . $action);
31
        }
32
    }
33
34
    /**
35
     * Handle a PostWasCreated event.
36
     *
37
     * @param \Xetaravel\Events\Discuss\PostWasCreatedEvent $event The event that was fired.
38
     *
39
     * @return bool
40
     */
41
    public function postWasCreated(PostWasCreatedEvent $event)
42
    {
43
        $data = [
44
            'obtainable_id' => $event->post->getKey(),
45
            'obtainable_type' => get_class($event->post),
46
            'event_type' => PostWasCreatedEvent::class
47
            /*'data' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
                'old' => $event->oldCategory,
49
                'new' => $event->category
50
            ]*/
51
        ];
52
53
        return $this->create($data);
54
    }
55
56
    /**
57
     * Create the log.
58
     *
59
     * @param array $data The data used to create the log.
60
     *
61
     * @return bool
62
     */
63 View Code Duplication
    protected function create(array $data) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        if (!isset($data['data'])) {
66
            $data['data'] = [];
67
        }
68
        $log = UserExperience::create($data);
69
70
        return !(is_null($log));
71
    }
72
}
73