FileSystemTicketStore   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 69
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTicket() 0 10 2
A deleteTicket() 0 6 2
A addTicket() 0 4 1
A __construct() 0 19 5
1
<?php
2
3
/*
4
 *    simpleSAMLphp-casserver is a CAS 1.0 and 2.0 compliant CAS server in the form of a simpleSAMLphp module
5
 *
6
 *    Copyright (C) 2013  Bjorn R. Jensen
7
 *
8
 *    This library is free software; you can redistribute it and/or
9
 *    modify it under the terms of the GNU Lesser General Public
10
 *    License as published by the Free Software Foundation; either
11
 *    version 2.1 of the License, or (at your option) any later version.
12
 *
13
 *    This library is distributed in the hope that it will be useful,
14
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 *    Lesser General Public License for more details.
17
 *
18
 *    You should have received a copy of the GNU Lesser General Public
19
 *    License along with this library; if not, write to the Free Software
20
 *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
 *
22
 */
23
24
declare(strict_types=1);
25
26
namespace SimpleSAML\Module\casserver\Cas\Ticket;
27
28
use Exception;
29
use SimpleSAML\Configuration;
30
31
class FileSystemTicketStore extends TicketStore
32
{
33
    /** @var string $pathToTicketDirectory */
34
    private string $pathToTicketDirectory;
35
36
37
    /**
38
     * @param \SimpleSAML\Configuration $config
39
     * @throws \Exception
40
     */
41
    public function __construct(Configuration $config)
42
    {
43
        $storeConfig = $config->getOptionalValue('ticketstore', ['directory' => 'ticketcache']);
44
45
        if (!is_string($storeConfig['directory'])) {
46
            throw new Exception('Invalid directory option in config.');
47
        }
48
49
        $path = $config->resolvePath($storeConfig['directory']);
50
51
        if (is_null($path) || !is_dir($path)) {
52
            throw new Exception('Directory for CAS Server ticket storage [' . strval($path) . '] does not exists.');
53
        }
54
55
        if (!is_writable($path)) {
56
            throw new Exception('Directory for CAS Server ticket storage [' . $path . '] is not writable.');
57
        }
58
59
        $this->pathToTicketDirectory = preg_replace('/\/$/', '', $path);
60
    }
61
62
63
    /**
64
     * @param string $ticketId
65
     * @return array|null
66
     */
67
    public function getTicket(string $ticketId): ?array
68
    {
69
        $filename = $this->pathToTicketDirectory . '/' . $ticketId;
70
71
        if (file_exists($filename)) {
72
            $content = file_get_contents($filename);
73
74
            return unserialize($content);
75
        } else {
76
            return null;
77
        }
78
    }
79
80
81
    /**
82
     * @param array $ticket
83
     */
84
    public function addTicket(array $ticket): void
85
    {
86
        $filename = $this->pathToTicketDirectory . '/' . $ticket['id'];
87
        file_put_contents($filename, serialize($ticket));
88
    }
89
90
91
    /**
92
     * @param string $ticketId
93
     */
94
    public function deleteTicket(string $ticketId): void
95
    {
96
        $filename = $this->pathToTicketDirectory . '/' . $ticketId;
97
98
        if (file_exists($filename)) {
99
            unlink($filename);
100
        }
101
    }
102
}
103