Lines 100.00% 32 / 32
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Session 100.00% 32 / 32 100.00% 4 / 4 8 100.00% 1 / 1
 readEntity 100.00% 9 / 9 100.00% 1 / 1 2
 updateEntity 100.00% 8 / 8 100.00% 1 / 1 1
 deleteEntity 100.00% 6 / 6 100.00% 1 / 1 2
 deleteByTimeInterval 100.00% 9 / 9 100.00% 1 / 1 3
1<?php
2
3namespace BO\Zmsbackend\Session\Service;
4
5use BO\Zmsentities\Session as Entity;
6
7class Session extends \BO\Zmsbackend\Base
8{
9    /**
10     * Fetch status from db
11     *
12     * @return \BO\Zmsentities\Session
13     */
14    public function readEntity(string $sessionName, string|int $sessionId)
15    {
16        $query = new \BO\Zmsbackend\Session\Repository\Session(\BO\Zmsbackend\Query\Base::SELECT);
17        $query
18            ->addEntityMapping()
19            ->addConditionSessionId($sessionId)
20            ->addConditionSessionName($sessionName);
21        $session = $this->fetchOne($query, new Entity());
22        if (!$session->hasId()) {
23            return null;
24        }
25        return $session;
26    }
27
28    public function updateEntity(Entity $session): Entity
29    {
30        $query = \BO\Zmsbackend\Session\Repository\Session::QUERY_WRITE;
31        $this->perform($query, array(
32            $session['id'],
33            $session['name'],
34            json_encode($session['content'])
35        ));
36        $entity = $this->readEntity($session['name'], $session['id']);
37        return $entity;
38    }
39
40    public function deleteEntity(string $sessionName, string|int $sessionId): bool
41    {
42        $query = \BO\Zmsbackend\Session\Repository\Session::QUERY_DELETE;
43        $result = $this->perform($query, array(
44            $sessionId,
45            $sessionName
46        ));
47        return ($result) ? true : false;
48    }
49
50    public function deleteByTimeInterval(?string $sessionName, int $deleteInSeconds): void
51    {
52        if ($sessionName !== null && $sessionName !== '') {
53            $this->perform(\BO\Zmsbackend\Session\Repository\Session::QUERY_DELETE_BY_AGE_AND_NAME, [
54                $deleteInSeconds,
55                $sessionName,
56            ]);
57            return;
58        }
59        $this->perform(\BO\Zmsbackend\Session\Repository\Session::QUERY_DELETE_BY_AGE, [
60            $deleteInSeconds,
61        ]);
62    }
63}