Passed
Push — master ( b4b8e8...3089dd )
by Arthur
26:17 queued 02:28
created

NotificationController::unread()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
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
    /**
21
     * @var NotificationService
22
     */
23
    protected $service;
24
25
    /**
26
     * NotificationController constructor.
27
     *
28
     * @param $service
29
     */
30 3
    public function __construct(NotificationServiceContract $service)
31
    {
32 3
        $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...
33 3
    }
34
35 1
    public function all()
36
    {
37 1
        return NotificationTransformer::collection($this->service->allNotificationsByUser(get_authenticated_user()));
38
    }
39
40 1
    public function allUnread()
41
    {
42 1
        return NotificationTransformer::collection($this->service->unreadNotifcationsByUser(get_authenticated_user()));
43
    }
44
45 2
    public function read($id)
46
    {
47 2
        if (!$this->service->find($id)->notifiable()->is(auth()->user())) {
48
            throw new NotFoundHttpException("notification not found");
49
        }
50
51 2
        $this->service->markAsRead($id);
52
53 2
        return response()->json([
54 2
            'success',
55
        ]);
56
    }
57
58
    public function unread($id)
59
    {
60
        if (!$this->service->find($id)->notifiable()->is(auth()->user())) {
61
            throw new NotFoundHttpException("notification not found");
62
        }
63
64
        $this->service->markAsUnread($id);
65
66
        return response()->json([
67
            'success',
68
        ]);
69
    }
70
}
71