Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
4.55% covered (danger)
4.55%
2 / 44
18.18% covered (danger)
18.18%
2 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
AvailabilityStatus
4.55% covered (danger)
4.55%
2 / 44
18.18% covered (danger)
18.18%
2 / 11
812.77
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
 isAvailable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 is
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isVisibleInHoldings
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStatusDescription
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 getExtraStatusInformation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStatusDescriptionTokens
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getSchemaAvailabilityUri
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 availabilityAsString
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 compareTo
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPriority
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * Availability Status Logic Class
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2024.
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  ILS_Logic
25 * @author   Ere Maijala <ere.maijala@helsinki.fi>
26 * @author   Thomas Wagener <wagener@hebis.uni-frankfurt.de>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org/wiki/development Wiki
29 */
30
31namespace VuFind\ILS\Logic;
32
33/**
34 * Availability Status Logic Class
35 *
36 * @category VuFind
37 * @package  ILS_Logic
38 * @author   Ere Maijala <ere.maijala@helsinki.fi>
39 * @author   Thomas Wagener <wagener@hebis.uni-frankfurt.de>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class AvailabilityStatus implements AvailabilityStatusInterface
44{
45    /**
46     * Items availability
47     *
48     * @var int
49     */
50    protected int $availability;
51
52    /**
53     * Constructor
54     *
55     * @param int|bool $availability           Availability
56     * @param string   $status                 Status Description
57     * @param array    $extraStatusInformation Extra Status Information
58     */
59    public function __construct(
60        int|bool $availability,
61        protected string $status = '',
62        protected array $extraStatusInformation = []
63    ) {
64        $this->availability = (int)$availability;
65    }
66
67    /**
68     * Check if available.
69     *
70     * @return bool
71     */
72    public function isAvailable(): bool
73    {
74        return $this->availability === self::STATUS_AVAILABLE || $this->availability === self::STATUS_UNCERTAIN;
75    }
76
77    /**
78     * Check if item has given availability status.
79     *
80     * @param int $availability Availability status
81     *
82     * @return bool
83     */
84    public function is(int $availability): bool
85    {
86        return $this->availability === $availability;
87    }
88
89    /**
90     * Check if status should be visible in the holdings tab.
91     *
92     * @return bool
93     */
94    public function isVisibleInHoldings(): bool
95    {
96        // Can be overridden if the status should not be visible in the holdings tab,
97        return true;
98    }
99
100    /**
101     * Get status description.
102     *
103     * @return string
104     */
105    public function getStatusDescription(): string
106    {
107        if (!empty($this->status)) {
108            return $this->status;
109        }
110        switch ($this->availability) {
111            case self::STATUS_UNAVAILABLE:
112                return 'Unavailable';
113            case self::STATUS_AVAILABLE:
114                return 'Available';
115            case self::STATUS_UNKNOWN:
116                return 'status_unknown_message';
117            default:
118                return 'Uncertain';
119        }
120    }
121
122    /**
123     * Get extra status information.
124     *
125     * @return array
126     */
127    public function getExtraStatusInformation(): array
128    {
129        return $this->extraStatusInformation;
130    }
131
132    /**
133     * Get status description tokens. Used when status description is being translated.
134     *
135     * @return array
136     */
137    public function getStatusDescriptionTokens(): array
138    {
139        $tokens = [];
140        foreach ($this->getExtraStatusInformation() as $key => $value) {
141            $tokens['%%' . $key . '%%'] = $value;
142        }
143        return $tokens;
144    }
145
146    /**
147     * Get schema.org availability URI.
148     *
149     * @return ?string
150     */
151    public function getSchemaAvailabilityUri(): ?string
152    {
153        switch ($this->availability) {
154            case self::STATUS_UNAVAILABLE:
155                return 'http://schema.org/OutOfStock';
156            case self::STATUS_AVAILABLE:
157                return 'http://schema.org/InStock';
158            case self::STATUS_UNKNOWN:
159                return null;
160            default:
161                return 'http://schema.org/LimitedAvailability';
162        }
163    }
164
165    /**
166     * Convert availability to a string
167     *
168     * @return string
169     */
170    public function availabilityAsString(): string
171    {
172        switch ($this->availability) {
173            case AvailabilityStatusInterface::STATUS_UNAVAILABLE:
174                return 'false';
175            case AvailabilityStatusInterface::STATUS_AVAILABLE:
176                return 'true';
177            case AvailabilityStatusInterface::STATUS_UNKNOWN:
178                return 'unknown';
179            default:
180                return 'uncertain';
181        }
182    }
183
184    /**
185     * Compares priority with other availability status for acquiring overall status of multiple status.
186     *
187     * @param AvailabilityStatusInterface $other Other Availability Status
188     *
189     * @return int
190     */
191    public function compareTo(AvailabilityStatusInterface $other): int
192    {
193        return $other->getPriority() <=> $this->getPriority();
194    }
195
196    /**
197     * Get status priority.
198     *
199     * @return int
200     */
201    protected function getPriority(): int
202    {
203        switch ($this->availability) {
204            case AvailabilityStatusInterface::STATUS_UNKNOWN:
205                return 0;
206            case AvailabilityStatusInterface::STATUS_UNAVAILABLE:
207                return 1;
208            case AvailabilityStatusInterface::STATUS_UNCERTAIN:
209                return 2;
210            default:
211                return 3;
212        }
213    }
214}