NotificationController::all()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 14.10.18
6
 * Time: 18:58.
7
 */
8
9
namespace Modules\Notification\Http\Controllers;
10
11
use Illuminate\Routing\Controller;
12
use Modules\Notification\Contracts\NotificationServiceContract;
13
use Modules\Notification\Services\NotificationService;
14
use Modules\Notification\Transformers\NotificationTransformer;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
17
class NotificationController extends Controller
18
{
19
    /**
20
     * @var NotificationService
21
     */
22
    protected $service;
23
24
    /**
25
     * NotificationController constructor.
26
     *
27
     * @param $service
28
     */
29
    public function __construct(NotificationServiceContract $service)
30
    {
31
        $this->service = $service;
0 ignored issues
show
Documentation Bug introduced by
$service is of type Modules\Notification\Con...ficationServiceContract, but the property $service was declared to be of type Modules\Notification\Services\NotificationService. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
32
    }
33
34
    public function all()
35
    {
36
        return NotificationTransformer::collection($this->service->allNotificationsByUser(get_authenticated_user()));
37
    }
38
39
    public function allUnread()
40
    {
41
        return NotificationTransformer::collection($this->service->unreadNotifcationsByUser(get_authenticated_user()));
42
    }
43
44
    public function read($id)
45
    {
46
        if (! $this->service->find($id)->notifiable()->is(auth()->user())) {
47
            throw new NotFoundHttpException('notification not found');
48
        }
49
50
        $this->service->markAsRead($id);
51
52
        return response()->json([
53
            'success',
54
        ]);
55
    }
56
57
    public function unread($id)
58
    {
59
        if (! $this->service->find($id)->notifiable()->is(auth()->user())) {
60
            throw new NotFoundHttpException('notification not found');
61
        }
62
63
        $this->service->markAsUnread($id);
64
65
        return response()->json([
66
            'success',
67
        ]);
68
    }
69
}
70