NotificationsController::markAsRead()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.439
c 0
b 0
f 0
cc 5
eloc 24
nc 4
nop 0
1
<?php
2
namespace App\Controller;
3
4
use Cake\Network\Exception\NotFoundException;
5
6
class NotificationsController extends AppController
7
{
8
9
    /**
10
     * Initialization hook method.
11
     *
12
     * @return void
13
     */
14
    public function initialize()
15
    {
16
        parent::initialize();
17
18
        $this->loadComponent('RequestHandler');
19
    }
20
21
    /**
22
     * Mark a notification as readed.
23
     *
24
     * @return void|\Cake\Network\Response
25
     */
26
    public function markAsRead()
27
    {
28
        if (!$this->request->is('ajax')) {
29
            throw new NotFoundException();
30
        }
31
32
        $this->Notifications = $this->loadModel('Notifications');
0 ignored issues
show
Documentation introduced by
The property Notifications does not exist on object<App\Controller\NotificationsController>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
33
34
        $json = [
35
            'error' => false
36
        ];
37
38
        $notification = $this->Notifications
0 ignored issues
show
Documentation introduced by
The property Notifications does not exist on object<App\Controller\NotificationsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
            ->find()
40
            ->where([
41
                'id' => $this->request->getData('id')
42
            ])
43
            ->first();
44
45
        //If the notification doesn't exist or if the owner of the
46
        //notification is not the current user, return an error.
47
        if (is_null($notification) || $notification->user_id != $this->Auth->user('id')) {
48
            $json['error'] = true;
49
50
            $this->set(compact('json'));
51
52
            $this->set('_serialize', 'json');
53
54
            return;
55
        }
56
57
        if ($notification->is_read) {
58
            $this->set(compact('json'));
59
            $this->set('_serialize', 'json');
60
61
            return;
62
        }
63
64
        $notification->is_read = 1;
65
        $this->Notifications->save($notification);
0 ignored issues
show
Documentation introduced by
The property Notifications does not exist on object<App\Controller\NotificationsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
67
        $this->set(compact('json'));
68
        $this->set('_serialize', ['json']);
69
    }
70
}
71