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
|
|
|
*/ |
31
|
|
|
class Readpath |
32
|
|
|
{ |
33
|
|
|
private $readpathRepository; |
34
|
|
|
|
35
|
|
|
/* @var Session */ |
36
|
|
|
private $session; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Readpath constructor. |
40
|
|
|
* @param ReadPathRepositoryInterface $readpathRepository |
41
|
|
|
* @param Session $session |
42
|
|
|
*/ |
43
|
7 |
|
public function __construct(ReadPathRepositoryInterface $readpathRepository, Session $session) |
44
|
|
|
{ |
45
|
7 |
|
$this->readpathRepository = $readpathRepository; |
46
|
7 |
|
$this->session = $session; |
47
|
7 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* saves a new readpath node to database |
51
|
|
|
* |
52
|
|
|
* @param Textnode $textnode |
53
|
|
|
* @param User|null $user |
54
|
|
|
*/ |
55
|
2 |
|
public function storeReadpath(Textnode $textnode, User $user = null) |
56
|
|
|
{ |
57
|
2 |
|
if (is_null($user)) { |
58
|
1 |
|
$this->saveTextnodeToSession($textnode); |
59
|
|
|
} else { |
60
|
1 |
|
$this->saveTextnodeToDatabase($textnode, $user); |
61
|
|
|
} |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param User|null $user |
66
|
|
|
* |
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
|
|
|
/** |
84
|
|
|
* @param Textnode $textnode |
85
|
|
|
* @param User $user |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
1 |
|
private function saveTextnodeToDatabase(Textnode $textnode, User $user): void |
90
|
|
|
{ |
91
|
1 |
|
$readpath = new ReadpathDocument(); |
92
|
1 |
|
$readpath->setTextnodeId($textnode->getId()); |
93
|
1 |
|
$readpath->setUserId($user->getId()); |
94
|
1 |
|
$readpath->setTimestamp(new \MongoDate(time())); |
95
|
|
|
|
96
|
1 |
|
$this->readpathRepository->save($readpath); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Textnode $textnode |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
1 |
|
private function saveTextnodeToSession(Textnode $textnode): void |
105
|
|
|
{ |
106
|
1 |
|
$readpath = $this->session->get('readpath'); |
107
|
1 |
|
if (is_array($readpath)) { |
108
|
1 |
|
$readpath[] = $textnode->getId(); |
109
|
|
|
} else { |
110
|
1 |
|
$readpath = [$textnode->getId()]; |
111
|
|
|
} |
112
|
1 |
|
$this->session->set('readpath', $readpath); |
113
|
1 |
|
} |
114
|
|
|
} |
115
|
|
|
|