OrderBy   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 49
wmc 8
lcom 1
cbo 0
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addOrderBy() 0 6 1
A toString() 0 18 4
A validateDirection() 0 8 2
1
<?php
2
3
namespace Muffin\Queries\Snippets;
4
5
use Muffin\Snippet;
6
7
class OrderBy implements Snippet
8
{
9
    const
10
        ASC = 'ASC',
11
        DESC = 'DESC';
12
13
    private
14
        $orders;
15
16 45
    public function __construct()
17
    {
18 45
        $this->orders = array();
19 45
    }
20
21 13
    public function addOrderBy($column, $direction = self::ASC)
22
    {
23 13
        $this->validateDirection($direction);
24
25 12
        $this->orders[$column] = (string) $direction;
26 12
    }
27
28 38
    public function toString()
29
    {
30 38
        $orders = array();
31 38
        foreach($this->orders as $column => $direction)
32
        {
33 12
            if(! empty($column))
34 12
            {
35 11
                $orders[] = $column . ' ' . $direction;
36 11
            }
37 38
        }
38
39 38
        if(empty($orders))
40 38
        {
41 29
            return '';
42
        }
43
44 11
        return sprintf('ORDER BY %s', implode(', ', $orders));
45
    }
46
47 13
    private function validateDirection($direction)
48
    {
49 13
        $availableDirections = array(self::ASC, self::DESC);
50 13
        if(! in_array($direction, $availableDirections))
51 13
        {
52 1
            throw new \InvalidArgumentException(sprintf('Unsupported ORDER BY direction "%s"', $direction));
53
        }
54 12
    }
55
}
56