Completed
Pull Request — master (#47)
by Phecho
04:34
created

NotePresenter::timestamp_iso()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 * 
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Presenters;
13
14
use Gitamin\Facades\Setting;
15
use Gitamin\Presenters\Traits\TimestampsTrait;
16
use GrahamCampbell\Markdown\Facades\Markdown;
17
use Jenssegers\Date\Date;
18
19 View Code Duplication
class NotePresenter extends AbstractPresenter
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    use TimestampsTrait;
22
23
    /**
24
     * Renders the message from Markdown into HTML.
25
     *
26
     * @return string
27
     */
28
    public function formattedMessage()
29
    {
30
        return Markdown::convertToHtml($this->wrappedObject->description);
31
    }
32
33
    /**
34
     * Present diff for humans date time.
35
     *
36
     * @return string
37
     */
38
    public function created_at_diff()
39
    {
40
        return (new Date($this->wrappedObject->created_at))
41
            ->setTimezone($this->setting->get('app_timezone'))
42
            ->diffForHumans();
43
    }
44
45
    /**
46
     * Present formatted date time.
47
     *
48
     * @return string
49
     */
50
    public function created_at_formatted()
51
    {
52
        return ucfirst((new Date($this->wrappedObject->created_at))
53
            ->setTimezone($this->setting->get('app_timezone'))
54
            ->format($this->setting->get('issue_date_format', 'l jS F Y H:i:s')));
55
    }
56
57
    /**
58
     * Formats the created_at time ready to be used by bootstrap-datetimepicker.
59
     *
60
     * @return string
61
     */
62
    public function created_at_datetimepicker()
63
    {
64
        return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i');
65
    }
66
67
    /**
68
     * Present formatted date time.
69
     *
70
     * @return string
71
     */
72
    public function created_at_iso()
73
    {
74
        return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String();
75
    }
76
77
    /**
78
     * Returns a formatted timestamp for use within the timeline.
79
     *
80
     * @return string
81
     */
82
    public function timestamp_formatted()
83
    {
84
        return $this->created_at_formatted;
0 ignored issues
show
Documentation introduced by
The property created_at_formatted does not exist on object<Gitamin\Presenters\NotePresenter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
85
    }
86
87
    /**
88
     * Return the iso timestamp for use within the timeline.
89
     *
90
     * @return string
91
     */
92
    public function timestamp_iso()
93
    {
94
        return $this->created_at_iso;
0 ignored issues
show
Documentation introduced by
The property created_at_iso does not exist on object<Gitamin\Presenters\NotePresenter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
    }
96
97
    /**
98
     * Present the status with an icon.
99
     *
100
     * @return string
101
     */
102
    public function icon()
103
    {
104
        switch ($this->wrappedObject->state) {
105
            case 0: // Scheduled
106
                return 'fa fa-calendar';
107
            case 1: // Investigating
108
                return 'fa fa-flag oranges';
109
            case 2: // Identified
110
                return 'fa fa-warning yellows';
111
            case 3: // Watching
112
                return 'fa fa-eye blues';
113
            case 4: // Fixed
114
                return 'fa fa-wrench greens';
115
            default: // Something actually broke, this shouldn't happen.
116
                return '';
117
        }
118
    }
119
120
    /**
121
     * Convert the presenter instance to an array.
122
     *
123
     * @return string[]
124
     */
125
    public function toArray()
126
    {
127
        return array_merge($this->wrappedObject->toArray(), [
128
            'created_at'   => $this->created_at(),
129
            'updated_at'   => $this->updated_at(),
130
        ]);
131
    }
132
}
133