Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
51.61% covered (warning)
51.61%
16 / 31
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageViewsTable
51.61% covered (warning)
51.61%
16 / 31
66.67% covered (warning)
66.67%
2 / 3
4.02
0.00% covered (danger)
0.00%
0 / 1
 initialize
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 validationDefault
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 buildRules
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3
4namespace App\Model\Table;
5
6use Cake\ORM\RulesChecker;
7use Cake\ORM\Table;
8use Cake\Validation\Validator;
9
10/**
11 * PageViews Model
12 *
13 * @property \App\Model\Table\ArticlesTable&\Cake\ORM\Association\BelongsTo $Articles
14 * @method \App\Model\Entity\PageView newEmptyEntity()
15 * @method \App\Model\Entity\PageView newEntity(array $data, array $options = [])
16 * @method array<\App\Model\Entity\PageView> newEntities(array $data, array $options = [])
17 * @method \App\Model\Entity\PageView get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
18 * @method \App\Model\Entity\PageView findOrCreate($search, ?callable $callback = null, array $options = [])
19 * @method \App\Model\Entity\PageView patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
20 * @method array<\App\Model\Entity\PageView> patchEntities(iterable $entities, array $data, array $options = [])
21 * @method \App\Model\Entity\PageView|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
22 * @method \App\Model\Entity\PageView saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
23 * @method iterable<\App\Model\Entity\PageView>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\PageView>|false saveMany(iterable $entities, array $options = [])
24 * @method iterable<\App\Model\Entity\PageView>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\PageView> saveManyOrFail(iterable $entities, array $options = [])
25 * @method iterable<\App\Model\Entity\PageView>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\PageView>|false deleteMany(iterable $entities, array $options = [])
26 * @method iterable<\App\Model\Entity\PageView>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\PageView> deleteManyOrFail(iterable $entities, array $options = [])
27 * @mixin \Cake\ORM\Behavior\TimestampBehavior
28 */
29class PageViewsTable extends Table
30{
31    /**
32     * Initialize method
33     *
34     * @param array<string, mixed> $config The configuration for the Table.
35     * @return void
36     */
37    public function initialize(array $config): void
38    {
39        parent::initialize($config);
40
41        $this->setTable('page_views');
42        $this->setDisplayField('ip_address');
43        $this->setPrimaryKey('id');
44
45        $this->addBehavior('Timestamp');
46
47        $this->belongsTo('Articles', [
48            'foreignKey' => 'article_id',
49            'joinType' => 'INNER',
50        ]);
51
52        $this->addBehavior('CounterCache', [
53            'Articles' => [
54                'view_count',
55            ],
56        ]);
57    }
58
59    /**
60     * Default validation rules.
61     *
62     * @param \Cake\Validation\Validator $validator Validator instance.
63     * @return \Cake\Validation\Validator
64     */
65    public function validationDefault(Validator $validator): Validator
66    {
67        $validator
68            ->uuid('article_id')
69            ->notEmptyString('article_id');
70
71        $validator
72            ->scalar('ip_address')
73            ->maxLength('ip_address', 45)
74            ->requirePresence('ip_address', 'create')
75            ->notEmptyString('ip_address');
76
77        $validator
78            ->scalar('user_agent')
79            ->allowEmptyString('user_agent');
80
81        $validator
82            ->scalar('referer')
83            ->allowEmptyString('referer');
84
85        return $validator;
86    }
87
88    /**
89     * Returns a rules checker object that will be used for validating
90     * application integrity.
91     *
92     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
93     * @return \Cake\ORM\RulesChecker
94     */
95    public function buildRules(RulesChecker $rules): RulesChecker
96    {
97        $rules->add($rules->existsIn(['article_id'], 'Articles'), ['errorField' => 'article_id']);
98
99        return $rules;
100    }
101}