Completed
Pull Request — master (#63)
by Phecho
04:21
created

MomentPresenter::created_at_iso()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
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\Models\Moment;
16
use Gitamin\Models\Comment;
17
use Gitamin\Models\Issue;
18
use Gitamin\Presenters\Traits\TimestampsTrait;
19
use GrahamCampbell\Markdown\Facades\Markdown;
20
use Jenssegers\Date\Date;
21
22
class MomentPresenter extends AbstractPresenter
23
{
24
    use TimestampsTrait;
25
26
    /**
27
     * Renders the message from Markdown into HTML.
28
     *
29
     * @return string
30
     */
31
    public function formattedData()
32
    {
33
        return Markdown::convertToHtml($this->wrappedObject->data);
34
    }
35
36
37
    public function formattedTarget()
38
    {
39
        if($this->wrappedObject->target instanceof Comment) {
40
            return Markdown::convertToHtml($this->wrappedObject->target->message);
0 ignored issues
show
Documentation introduced by
The property message does not exist on object<Gitamin\Models\Comment>. 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...
41
        } elseif ($this->wrappedObject->target instanceof Issue) {
42
            return Markdown::convertToHtml($this->wrappedObject->target->description);
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Gitamin\Models\Issue>. 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...
43
        }
44
    }
45
46
    /** 
47
     * Get the moment action summary
48
     *
49
     * @return string
50
     */
51
    protected function actionName()
52
    {
53
        switch ($this->wrappedObject->action) {
54
            case Moment::CREATED:
55
                return 'created';
56
            case Moment::CLOSED:
57
                return 'closed';
58
            case Moment::COMMENTED:
59
                return 'commented on';
60
            default:
61
                return 'Unknow';
62
        }
63
    }
64
65
    public function icon()
66
    {
67
        if($this->wrappedObject->target instanceof Comment) {
68
            return 'fa fa-comments-o';
69
        } elseif ($this->wrappedObject->target instanceof Issue) {
70
            return 'fa fa-exclamation-circle';
71
        } else {
72
            return 'fa fa-code-fork';
73
        }
74
    }
75
    /**
76
     * Present formatted date time.
77
     *
78
     * @return string
79
     */
80
    public function created_at_iso()
81
    {
82
        return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String();
83
    }
84
85
    /**
86
     * Convert presented moment to an array.
87
     *
88
     * @return array
89
     */
90
    public function toArray()
91
    {
92
        return [
93
            'summary'        => $this->summary(),
0 ignored issues
show
Documentation Bug introduced by
The method summary does not exist on object<Gitamin\Presenters\MomentPresenter>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
94
            'created_at_iso' => $this->created_at_iso(),
95
        ];
96
    }
97
}
98