Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ShortlinksService
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 7
56
0.00% covered (danger)
0.00%
0 / 1
 beginTransaction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 commitTransaction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 rollBackTransaction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createEntity
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createAndPersistEntityForPath
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getShortLinkByHash
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getShortLinksWithMissingHashes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Database service for shortlinks.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2023.
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  Database
25 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
29 */
30
31namespace VuFind\Db\Service;
32
33use Exception;
34use VuFind\Db\Entity\ShortlinksEntityInterface;
35use VuFind\Db\Table\DbTableAwareInterface;
36use VuFind\Db\Table\DbTableAwareTrait;
37
38/**
39 * Database service for shortlinks.
40 *
41 * @category VuFind
42 * @package  Database
43 * @author   Sudharma Kellampalli <skellamp@villanova.edu>
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org/wiki/development:plugins:database_gateways Wiki
47 */
48class ShortlinksService extends AbstractDbService implements
49    DbTableAwareInterface,
50    ShortlinksServiceInterface,
51    Feature\TransactionInterface
52{
53    use DbTableAwareTrait;
54
55    /**
56     * Begin a database transaction.
57     *
58     * @return void
59     * @throws Exception
60     */
61    public function beginTransaction(): void
62    {
63        $this->getDbTable('shortlinks')->beginTransaction();
64    }
65
66    /**
67     * Commit a database transaction.
68     *
69     * @return void
70     * @throws Exception
71     */
72    public function commitTransaction(): void
73    {
74        $this->getDbTable('shortlinks')->commitTransaction();
75    }
76
77    /**
78     * Roll back a database transaction.
79     *
80     * @return void
81     * @throws Exception
82     */
83    public function rollBackTransaction(): void
84    {
85        $this->getDbTable('shortlinks')->rollbackTransaction();
86    }
87
88    /**
89     * Create a short link entity.
90     *
91     * @return ShortlinksEntityInterface
92     */
93    public function createEntity(): ShortlinksEntityInterface
94    {
95        return $this->getDbTable('shortlinks')->createRow();
96    }
97
98    /**
99     * Create and persist an entity for the provided path.
100     *
101     * @param string $path Path part of URL being shortened.
102     *
103     * @return ShortlinksEntityInterface
104     */
105    public function createAndPersistEntityForPath(string $path): ShortlinksEntityInterface
106    {
107        $table = $this->getDbTable('shortlinks');
108        $table->insert(['path' => $path]);
109        $id = $table->getLastInsertValue();
110        return $table->select(['id' => $id])->current();
111    }
112
113    /**
114     * Look up a short link by hash value.
115     *
116     * @param string $hash Hash value.
117     *
118     * @return ?ShortlinksEntityInterface
119     */
120    public function getShortLinkByHash(string $hash): ?ShortlinksEntityInterface
121    {
122        return $this->getDbTable('shortlinks')->select(['hash' => $hash])->current();
123    }
124
125    /**
126     * Get rows with missing hashes (for legacy upgrading).
127     *
128     * @return ShortlinksEntityInterface[]
129     */
130    public function getShortLinksWithMissingHashes(): array
131    {
132        return iterator_to_array($this->getDbTable('shortlinks')->select(['hash' => null]));
133    }
134}