1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2017 Michael Giesler |
3
|
|
|
* |
4
|
|
|
* This file is part of Dembelo. |
5
|
|
|
* |
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License 3 for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DembeloMain\Model; |
21
|
|
|
|
22
|
|
|
use DembeloMain\Document\Textnode; |
23
|
|
|
use DembeloMain\Document\User; |
24
|
|
|
use DembeloMain\Model\Repository\ReadPathRepositoryInterface; |
25
|
|
|
use DembeloMain\Document\Readpath as ReadpathDocument; |
26
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Readpath |
30
|
|
|
* @package DembeloMain\Model |
31
|
|
|
*/ |
32
|
|
|
class Readpath |
33
|
|
|
{ |
34
|
|
|
private $readpathRepository; |
35
|
|
|
|
36
|
|
|
/* @var Session */ |
37
|
|
|
private $session; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Readpath constructor. |
41
|
|
|
* @param ReadPathRepositoryInterface $readpathRepository |
42
|
|
|
* @param Session $session |
43
|
|
|
*/ |
44
|
7 |
|
public function __construct(ReadPathRepositoryInterface $readpathRepository, Session $session) |
45
|
|
|
{ |
46
|
7 |
|
$this->readpathRepository = $readpathRepository; |
47
|
7 |
|
$this->session = $session; |
48
|
7 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* saves a new readpath node to database |
52
|
|
|
* |
53
|
|
|
* @param Textnode $textnode |
54
|
|
|
* @param User|null $user |
55
|
|
|
*/ |
56
|
2 |
|
public function storeReadpath(Textnode $textnode, User $user = null) |
57
|
|
|
{ |
58
|
2 |
|
if (is_null($user)) { |
59
|
1 |
|
$this->saveTextnodeToSession($textnode); |
60
|
|
|
} else { |
61
|
1 |
|
$this->saveTextnodeToDatabase($textnode, $user); |
62
|
|
|
} |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param User|null $user |
67
|
|
|
* @return null|string |
68
|
|
|
*/ |
69
|
5 |
|
public function getCurrentTextnodeId(User $user = null): ?string |
70
|
|
|
{ |
71
|
5 |
|
if (is_null($user)) { |
72
|
3 |
|
$readpath = $this->session->get('readpath'); |
73
|
3 |
|
if (!is_array($readpath)) { |
74
|
1 |
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return end($readpath); |
78
|
|
|
} else { |
79
|
2 |
|
return $this->readpathRepository->getCurrentTextnodeIdForUser($user); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
private function saveTextnodeToDatabase(Textnode $textnode, User $user) |
84
|
|
|
{ |
85
|
1 |
|
$readpath = new ReadpathDocument(); |
86
|
1 |
|
$readpath->setTextnodeId($textnode->getId()); |
87
|
1 |
|
$readpath->setUserId($user->getId()); |
88
|
1 |
|
$readpath->setTimestamp(new \MongoDate(time())); |
89
|
|
|
|
90
|
1 |
|
$this->readpathRepository->save($readpath); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
1 |
|
private function saveTextnodeToSession($textnode) |
94
|
|
|
{ |
95
|
1 |
|
$readpath = $this->session->get('readpath'); |
96
|
1 |
|
if (is_array($readpath)) { |
97
|
1 |
|
$readpath[] = $textnode->getId(); |
98
|
|
|
} else { |
99
|
1 |
|
$readpath = [$textnode->getId()]; |
100
|
|
|
} |
101
|
1 |
|
$this->session->set('readpath', $readpath); |
102
|
1 |
|
} |
103
|
|
|
} |
104
|
|
|
|