Completed
Pull Request — master (#1)
by Sébastien
02:48
created

TrelloItemNotFoundException   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getNotFoundMessage() 0 8 2
A getItemName() 0 4 1
A getItemType() 0 4 1
1
<?php
2
3
namespace TrelloBurndown\Exception;
4
5
/**
6
 * Class TrelloItemNotFoundException.
7
 */
8
class TrelloItemNotFoundException extends \Exception
9
{
10
    /**
11
     * @var array
12
     */
13
    private static $itemTypes = [
14
        'board',
15
        'list',
16
        'card',
17
    ];
18
19
    /**
20
     * @var string
21
     */
22
    private $itemType;
23
24
    /**
25
     * @var string
26
     */
27
    private $itemName;
28
29
    /**
30
     * TrelloItemNotFoundException constructor.
31
     *
32
     * @param string          $itemType
33
     * @param string          $itemName
34
     * @param int             $code
35
     * @param \Exception|null $previous
36
     */
37
    public function __construct(String $itemType, String $itemName, $code = 0, \Exception $previous = null)
38
    {
39
        $this->itemName = $itemName;
40
        $this->itemType = $itemType;
41
42
        try {
43
            $message = self::getNotFoundMessage($itemType, $itemName);
44
            parent::__construct($message, 404, $previous);
45
        } catch (\Exception $e) {
46
            parent::__construct($e->getMessage());
47
        }
48
    }
49
50
    /**
51
     * @param $itemType
52
     * @param $itemName
53
     *
54
     * @return string
55
     *
56
     * @throws \Exception
57
     */
58
    private static function getNotFoundMessage($itemType, $itemName)
59
    {
60
        if (in_array($itemType, self::$itemTypes)) {
61
            return 'The trello '.$itemType.' named '.$itemName.' could not be found.';
62
        }
63
64
        throw new \Exception('The declare Trello item type does not match any known type.');
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getItemName()
71
    {
72
        return $this->itemName;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getItemType()
79
    {
80
        return $this->itemType;
81
    }
82
}
83