BookmarksTable::createBookmark()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Bookmarks\Model\Table;
14
15
use App\Model\Table\EntriesTable;
16
use App\Model\Table\UsersTable;
17
use Cake\ORM\Table;
18
use Cake\Validation\Validator;
19
20
/**
21
 * Bookmarks Table
22
 *
23
 * @property EntriesTable $Entries
24
 * @property UsersTable $Users
25
 */
26
class BookmarksTable extends Table
27
{
28
29
    /**
30
     * {@inheritdoc}
31
     *
32
     * @param array $config config
33
     * @return void
34
     */
35
    public function initialize(array $config)
36
    {
37
        $this->addBehavior('Timestamp');
38
39
        $this->belongsTo('Entries', ['foreignKey' => 'entry_id']);
40
        $this->belongsTo('Users', ['foreignKey' => 'user_id']);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @param \Cake\Validation\Validator $validator validator
47
     * @return \Cake\Validation\Validator
48
     */
49
    public function validationDefault(Validator $validator)
50
    {
51
        $validator
52
            ->requirePresence('entry_id', 'create')
53
            ->add(
54
                'entry_id',
55
                [
56
                    'exists' => [
57
                        'rule' => [$this, 'validatePostingExists'],
58
                        'last' => true,
59
                    ],
60
                    'numeric' => ['rule' => 'numeric', 'last' => true],
61
                    'unique' => [
62
                        'rule' => [$this, 'validateUniqueBookmark'],
63
                        'last' => true,
64
                        'on' => 'create',
65
                    ],
66
                ]
67
            )
68
            ->requirePresence('user_id', 'create')
69
            ->add(
70
                'user_id',
71
                [
72
                    'exists' => [
73
                        'rule' => [$this, 'validateUserExists'],
74
                        'last' => true,
75
                    ],
76
                    'numeric' => ['rule' => 'numeric', 'last' => true],
77
                ]
78
            );
79
80
        return $validator;
81
    }
82
83
    /**
84
     * Check if user exists.
85
     *
86
     * @param int $value user-ID
87
     * @return bool valid
88
     */
89
    public function validateUserExists($value)
90
    {
91
        return $this->Users->exists(['id' => $value]);
92
    }
93
94
    /**
95
     * Check if posting exists.
96
     *
97
     * @param int $value posting-ID
98
     * @return bool valid
99
     */
100
    public function validatePostingExists($value)
101
    {
102
        return $this->Entries->exists(['id' => $value]);
103
    }
104
105
    /**
106
     * Validate that combination of entry_id and user_id is unique.
107
     *
108
     * @param int $value value
109
     * @param array $context context
110
     * @return bool
111
     */
112
    public function validateUniqueBookmark($value, $context)
113
    {
114
        $data = $context['data'];
115
        if (empty($data['user_id'])) {
116
            return true;
117
        }
118
        $conditions = [
119
            'entry_id' => $data['entry_id'],
120
            'user_id' => $data['user_id'],
121
        ];
122
123
        return !$this->exists($conditions);
124
    }
125
126
    /**
127
     * Create and save new bookmark.
128
     *
129
     * @param array $data bookmark data
130
     * @return bool|\Cake\Datasource\EntityInterface
131
     */
132
    public function createBookmark(array $data)
133
    {
134
        $bookmark = $this->newEntity(
135
            $data,
136
            ['fields' => ['entry_id', 'user_id']]
137
        );
138
        if ($bookmark->getErrors()) {
139
            return $bookmark;
140
        }
141
142
        return $this->save($bookmark);
143
    }
144
}
145