Completed
Push — master ( 6f85e7...580597 )
by Ariel
07:05
created

AppointmentPresenter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Timegridio\Concierge\Presenters;
4
5
use McCool\LaravelAutoPresenter\BasePresenter;
6
use Timegridio\Concierge\Models\Appointment;
7
8
class AppointmentPresenter extends BasePresenter
9
{
10 6
    public function __construct(Appointment $resource)
11
    {
12 6
        $this->wrappedObject = $resource;
13 6
    }
14
15 1
    public function code()
16
    {
17 1
        $length = $this->wrappedObject->business->pref('appointment_code_length');
18
19 1
        return strtoupper(substr($this->wrappedObject->hash, 0, $length));
20
    }
21
22 3
    public function date($format = 'Y-m-d')
23
    {
24 3
        if ($this->wrappedObject->start_at->isToday()) {
25 1
            return studly_case(trans('Concierge::appointments.text.today'));
0 ignored issues
show
Bug introduced by
It seems like trans('Concierge::appointments.text.today') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, studly_case() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
26
        }
27
28 2
        if ($this->wrappedObject->start_at->isTomorrow()) {
29 1
            return studly_case(trans('Concierge::appointments.text.tomorrow'));
0 ignored issues
show
Bug introduced by
It seems like trans('Concierge::appointments.text.tomorrow') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, studly_case() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
30
        }
31
32 1
        return $this->wrappedObject
33 1
                    ->start_at
34 1
                    ->timezone($this->wrappedObject->business->timezone)
35 1
                    ->format($format);
36 1
    }
37
38
    public function arriveAt()
39
    {
40
        if (!$this->wrappedObject->business->pref('appointment_flexible_arrival')) {
41
            return $this->time;
0 ignored issues
show
Documentation introduced by
The property time does not exist on object<Timegridio\Concie...s\AppointmentPresenter>. 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...
42
        }
43
44
        $fromTime = $this->wrappedObject
45
                         ->vacancy
46
                         ->start_at
47
                         ->timezone($this->wrappedObject->business->timezone)
48
                         ->format(config('root.time.format'));
49
50
        $toTime = $this->wrappedObject
51
                       ->vacancy
52
                       ->finish_at
53
                       ->timezone($this->wrappedObject->business->timezone)
54
                       ->format(config('root.time.format'));
55
56
        return ucwords(trans('Concierge::appointments.text.from_to', ['from' => $fromTime, 'to' => $toTime]));
57
    }
58
59
    public function time()
60
    {
61
        return $this->wrappedObject
62
                    ->start_at
63
                    ->timezone($this->wrappedObject->business->timezone)
64
                    ->format(config('root.time.format'));
65
    }
66
67
    public function finishTime()
68
    {
69
        return $this->wrappedObject
70
                    ->finish_at
71
                    ->timezone($this->wrappedObject->business->timezone)
72
                    ->format(config('root.time.format'));
73
    }
74
75
    public function diffForHumans()
76
    {
77
        return $this->wrappedObject->start_at->timezone($this->wrappedObject->business->timezone)->diffForHumans();
78
    }
79
80
    public function phone()
81
    {
82
        return $this->wrappedObject->business->phone;
83
    }
84
85
    public function location()
86
    {
87
        return $this->wrappedObject->business->postal_address;
88
    }
89
90
    public function statusLetter()
91
    {
92
        return substr(trans('Concierge::appointments.status.'.$this->wrappedObject->statusLabel), 0, 1);
93
    }
94
95
    public function status()
96
    {
97
        return trans('Concierge::appointments.status.'.$this->wrappedObject->statusLabel);
98
    }
99
100
    public function statusIcon()
101
    {
102
        return '<span class="label label-'.$this->statusToCssClass().'">'.$this->statusLetter.'</span>';
0 ignored issues
show
Documentation introduced by
The property statusLetter does not exist on object<Timegridio\Concie...s\AppointmentPresenter>. 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...
103
    }
104
105 1
    public function statusToCssClass()
106
    {
107 1
        switch ($this->wrappedObject->status) {
108 1
            case Appointment::STATUS_ANNULATED:
109 1
                return 'danger';
110
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
111 1
            case Appointment::STATUS_CONFIRMED:
112 1
                return 'success';
113
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
114 1
            case Appointment::STATUS_RESERVED:
115 1
                return 'warning';
116
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
117 1
            case Appointment::STATUS_SERVED:
118 1
            default:
119 1
                return 'default';
120
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
121
        }
122
    }
123
124
    public function panel()
125
    {
126
        return view('widgets.appointment.panel._body', ['appointment' => $this, 'user' => auth()->user()])->render();
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
127
    }
128
129
    public function row()
130
    {
131
        return view('widgets.appointment.row._body', ['appointment' => $this, 'user' => auth()->user()])->render();
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
132
    }
133
}
134