Completed
Push — master ( a3a70f...f040df )
by Julius
11s
created

DefaultBoardService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 9
dl 0
loc 86
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A checkFirstRun() 0 11 3
B createDefaultBoard() 0 30 7
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Ryan Fletcher <[email protected]>
4
 *
5
 * @author Ryan Fletcher <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Service;
25
26
use OCA\Deck\Db\BoardMapper;
27
use OCA\Deck\Service\BoardService;
28
use OCA\Deck\Service\StackService;
29
use OCA\Deck\Service\CardService;
30
use OCP\IConfig;
31
use OCP\IL10N;
32
use OCA\Deck\BadRequestException;
33
34
class DefaultBoardService {
35
36
	private $boardMapper;
37
	private $boardService;
38
	private $stackService;
39
	private $cardService;
40
	private $config;
41
	private $l10n;
42
43
    public function __construct(
44
			IL10N $l10n,
45
			BoardMapper $boardMapper,
46
			BoardService $boardService,
47
			StackService $stackService,
48
			CardService $cardService,
49
			IConfig $config
50
			) {
51
52
		$this->boardService = $boardService;
53
		$this->stackService = $stackService;
54
		$this->cardService = $cardService;
55
		$this->config = $config;
56
		$this->boardMapper = $boardMapper;
57
		$this->l10n = $l10n;
58
    }
59
60
	/**
61
	 * @param $userId
62
	 * @param $appName
63
	 * @return bool
64
	 * @throws \OCP\PreConditionNotMetException
65
	 */
66
	public function checkFirstRun($userId, $appName) {
67
		$firstRun = $this->config->getUserValue($userId, $appName, 'firstRun', 'yes');
68
		$userBoards = $this->boardMapper->findAllByUser($userId);
69
		
70
		if ($firstRun === 'yes' && count($userBoards) === 0) {
71
			$this->config->setUserValue($userId, $appName, 'firstRun', 'no');
72
			return true;
73
		}
74
75
		return false;
76
    }
77
78
	/**
79
	 * @param $title
80
	 * @param $userId
81
	 * @param $color
82
	 * @return \OCP\AppFramework\Db\Entity
83
	 * @throws \OCA\Deck\NoPermissionException
84
	 * @throws \OCA\Deck\StatusException
85
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
86
	 * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException
87
	 * @throws BadRequestException
88
	 */
89
	public function createDefaultBoard($title, $userId, $color) {
90
91
		if ($title === false || $title === null) {
92
			throw new BadRequestException('title must be provided');
93
		}
94
95
		if ($userId === false || $userId === null) {
96
			throw new BadRequestException('userId must be provided');
97
		}
98
99
		if ($color === false || $color === null) {
100
			throw new BadRequestException('color must be provided');
101
		}
102
103
        $defaultBoard = $this->boardService->create($title, $userId, $color);
104
        $defaultStacks = [];
105
        $defaultCards = [];
106
107
		$boardId = $defaultBoard->getId();		
108
109
		$defaultStacks[] = $this->stackService->create($this->l10n->t('To do'), $boardId, 1);
110
		$defaultStacks[] = $this->stackService->create($this->l10n->t('Doing'), $boardId, 1);
111
		$defaultStacks[] = $this->stackService->create($this->l10n->t('Done'), $boardId, 1);
112
        
113
		$defaultCards[] = $this->cardService->create($this->l10n->t('Example Task 3'), $defaultStacks[0]->getId(), 'text', 0, $userId);
114
		$defaultCards[] = $this->cardService->create($this->l10n->t('Example Task 2'), $defaultStacks[1]->getId(), 'text', 0, $userId);
115
		$defaultCards[] = $this->cardService->create($this->l10n->t('Example Task 1'), $defaultStacks[2]->getId(), 'text', 0, $userId);
116
117
		return $defaultBoard;
118
    }
119
}