Lines 100.00% 38 / 38
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% 38 / 38 100.00% 4 / 4 9 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% 15 / 15 100.00% 1 / 1 4
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($sessionName, $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($sessionName, $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        $selectQuery = new \BO\Zmsbackend\Session\Repository\Session(\BO\Zmsbackend\Query\Base::SELECT);
53        $selectQuery
54            ->addEntityMapping()
55            ->addConditionSessionDeleteInterval($deleteInSeconds);
56        if ($sessionName !== null && $sessionName !== '') {
57            $selectQuery->addConditionSessionName($sessionName);
58        }
59        $statement = $this->fetchStatement($selectQuery);
60        while ($sessionData = $statement->fetch(\PDO::FETCH_ASSOC)) {
61            $sessionData = (new \BO\Zmsbackend\Session\Repository\Session(\BO\Zmsbackend\Query\Base::SELECT))->postProcessJoins($sessionData);
62            $entity = new Entity($sessionData);
63            $deleteQuery = new \BO\Zmsbackend\Session\Repository\Session(\BO\Zmsbackend\Query\Base::DELETE);
64            $deleteQuery
65                ->addConditionSessionName($entity['name'])
66                ->addConditionSessionId($entity['id']);
67            $this->deleteItem($deleteQuery);
68        }
69    }
70}