Tags   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getList() 0 26 5
1
<?php
2
3
namespace Harentius\BlogBundle\Sidebar;
4
5
use Harentius\BlogBundle\Entity\TagRepository;
6
7
class Tags
8
{
9
    /**
10
     * @var TagRepository
11
     */
12
    private $tagRepository;
13
14
    /**
15
     * @var int
16
     */
17
    private $sidebarTagsLimit;
18
19
    /**
20
     * @var array
21
     */
22
    private $sidebarTagSizes;
23
24
    /**
25
     * @param TagRepository $tagRepository
26
     * @param int $sidebarTagsLimit
27
     * @param array $sidebarTagSizes
28
     */
29
    public function __construct(TagRepository $tagRepository, int $sidebarTagsLimit, array $sidebarTagSizes)
30
    {
31
        $this->tagRepository = $tagRepository;
32
        $this->sidebarTagsLimit = $sidebarTagsLimit;
33
        $this->sidebarTagSizes = $sidebarTagSizes;
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function getList()
40
    {
41
        $tags = $this->tagRepository->findMostPopularLimited($this->sidebarTagsLimit);
42
43
        if (!$tags) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tags of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
44
            return $tags;
45
        }
46
47
        $maxWeight = $tags[0]['weight'];
48
49
        foreach ($tags as $key => $tag) {
50
            $percentage = 100;
51
            $minDiff = 1;
52
53
            foreach ($this->sidebarTagSizes as $tagPercent) {
54
                if (($diff = abs($tag['weight'] / $maxWeight - $tagPercent / 100)) < $minDiff) {
55
                    $minDiff = $diff;
56
                    $percentage = $tagPercent;
57
                }
58
            }
59
60
            $tags[$key]['percentage'] = $percentage;
61
        }
62
63
        return $tags;
64
    }
65
}
66