ForumHelper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 35
c 3
b 0
f 0
dl 0
loc 100
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBreadcrumb() 0 13 2
A __construct() 0 3 1
A listBoardsFormSelect() 0 26 5
A urlExists() 0 10 2
1
<?php
2
3
namespace App\Service;
4
5
use App\Entity\Board;
6
use App\Entity\Forum;
7
use Doctrine\ORM\EntityManagerInterface;
8
9
class ForumHelper
10
{
11
    public const DEFAULT_SHOW_THREAD_COUNT = 10;
12
13
    public static $settings = [
14
        'forum' => [
15
            'name' => [
16
                'min_length' => 4,
17
            ],
18
            'url' => [
19
                'min_length' => 3,
20
            ],
21
        ],
22
    ];
23
24
    /**
25
     * @var EntityManagerInterface
26
     */
27
    private $em;
28
29
    public function __construct(EntityManagerInterface $manager)
30
    {
31
        $this->em = $manager;
32
    }
33
34
    /**
35
     * Checks whether the given url exists, in other words, if the forum exists.
36
     *
37
     * @param string $url
38
     *
39
     * @return bool
40
     */
41
    public function urlExists($url)
42
    {
43
        /** @var Forum[] $find */
44
        $find = $this->em->getRepository(Forum::class)->findBy(['url' => $url]);
45
46
        if (count($find)) {
47
            return true;
48
        }
49
50
        return false;
51
    }
52
53
    /**
54
     * Get a breadcrumb for the current tree.
55
     *
56
     * @param Board $board
57
     *
58
     * @return Board[]
59
     */
60
    public function getBreadcrumb($board)
61
    {
62
        $boardsList = [];
63
        $parentBoard = $board->getParentBoard();
64
65
        while (null !== $parentBoard) {
66
            $next = $parentBoard;
67
            array_unshift($boardsList, $next);
68
            $board = $next;
69
            $parentBoard = $board->getParentBoard();
70
        }
71
72
        return $boardsList;
73
    }
74
75
    /**
76
     * @param Forum      $forum
77
     * @param Board|null $board
78
     * @param int        $level
79
     * @param array      $list
80
     *
81
     * @return array
82
     */
83
    public function listBoardsFormSelect($forum, $board, $level = 1, &$list = [])
84
    {
85
        /** @var Board[] $boardList */
86
        $boardList = $this->em->getRepository(Board::class)->findBy(['forum' => $forum, 'parent_board' => $board]);
87
88
        if (empty($list)) {
89
            $list['- Root (ID: 0)'] = 0;
90
        }
91
92
        foreach ($boardList as $currentBoard) {
93
            $line = '-';
94
            for ($i = mb_strlen($line) - 1; $i < $level; ++$i) {
95
                $line .= '-';
96
            }
97
98
            $title = $line . ' ' . $currentBoard->getTitle() . ' (ID: ' . $currentBoard->getId() . ')';
99
            $list[$title] = $currentBoard->getId();
100
101
            // Has sub-boards
102
            if (count($currentBoard->getBoards())) {
103
                $nextLevel = $level + 1;
104
                $this->listBoardsFormSelect($forum, $currentBoard, $nextLevel, $list);
105
            }
106
        }
107
108
        return $list;
109
    }
110
}
111