Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.56% covered (danger)
5.56%
1 / 18
10.00% covered (danger)
10.00%
1 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Ratings
5.56% covered (danger)
5.56%
1 / 18
10.00% covered (danger)
10.00%
1 / 10
133.31
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUser
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setUser
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getResource
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setResource
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getRating
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setRating
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getCreated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCreated
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Row Definition for ratings
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2022.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Db_Row
25 * @author   Ere Maijala <ere.maijala@helsinki.fi>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Db\Row;
31
32use DateTime;
33use VuFind\Db\Entity\RatingsEntityInterface;
34use VuFind\Db\Entity\ResourceEntityInterface;
35use VuFind\Db\Entity\UserEntityInterface;
36use VuFind\Db\Service\DbServiceAwareInterface;
37use VuFind\Db\Service\DbServiceAwareTrait;
38use VuFind\Db\Service\ResourceServiceInterface;
39use VuFind\Db\Service\UserServiceInterface;
40
41/**
42 * Row Definition for ratings
43 *
44 * @category VuFind
45 * @package  Db_Row
46 * @author   Ere Maijala <ere.maijala@helsinki.fi>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org Main Site
49 *
50 * @property int    $id
51 * @property int    $user_id
52 * @property int    $resource_id
53 * @property int    $rating
54 * @property string $created
55 */
56class Ratings extends RowGateway implements
57    \VuFind\Db\Entity\RatingsEntityInterface,
58    \VuFind\Db\Table\DbTableAwareInterface,
59    DbServiceAwareInterface
60{
61    use \VuFind\Db\Table\DbTableAwareTrait;
62    use DbServiceAwareTrait;
63
64    /**
65     * Constructor
66     *
67     * @param \Laminas\Db\Adapter\Adapter $adapter Database adapter
68     */
69    public function __construct($adapter)
70    {
71        parent::__construct('id', 'ratings', $adapter);
72    }
73
74    /**
75     * Get identifier (returns null for an uninitialized or non-persisted object).
76     *
77     * @return ?int
78     */
79    public function getId(): ?int
80    {
81        return $this->id ?? null;
82    }
83
84    /**
85     * Get user.
86     *
87     * @return ?UserEntityInterface
88     */
89    public function getUser(): ?UserEntityInterface
90    {
91        return $this->user_id
92            ? $this->getDbServiceManager()->get(UserServiceInterface::class)->getUserById($this->user_id)
93            : null;
94    }
95
96    /**
97     * Set user.
98     *
99     * @param ?UserEntityInterface $user User
100     *
101     * @return RatingsEntityInterface
102     */
103    public function setUser(?UserEntityInterface $user): RatingsEntityInterface
104    {
105        $this->user_id = $user?->getId();
106        return $this;
107    }
108
109    /**
110     * Get resource.
111     *
112     * @return ResourceEntityInterface
113     */
114    public function getResource(): ResourceEntityInterface
115    {
116        return $this->resource_id
117        ? $this->getDbServiceManager()->get(ResourceServiceInterface::class)->getResourceById($this->resource_id)
118        : null;
119    }
120
121    /**
122     * Set resource.
123     *
124     * @param ResourceEntityInterface $resource Resource
125     *
126     * @return RatingsEntityInterface
127     */
128    public function setResource(ResourceEntityInterface $resource): RatingsEntityInterface
129    {
130        $this->resource_id = $resource->getId();
131        return $this;
132    }
133
134    /**
135     * Get rating.
136     *
137     * @return int
138     */
139    public function getRating(): int
140    {
141        return $this->rating ?? '';
142    }
143
144    /**
145     * Set rating.
146     *
147     * @param int $rating Rating
148     *
149     * @return RatingsEntityInterface
150     */
151    public function setRating(int $rating): RatingsEntityInterface
152    {
153        $this->rating = $rating;
154        return $this;
155    }
156
157    /**
158     * Get created date.
159     *
160     * @return DateTime
161     */
162    public function getCreated(): DateTime
163    {
164        return DateTime::createFromFormat('Y-m-d H:i:s', $this->created);
165    }
166
167    /**
168     * Set created date.
169     *
170     * @param DateTime $dateTime Created date
171     *
172     * @return RatingsEntityInterface
173     */
174    public function setCreated(DateTime $dateTime): RatingsEntityInterface
175    {
176        $this->created = $dateTime->format('Y-m-d H:i:s');
177        return $this;
178    }
179}