Completed
Push — master ( a1a35a...2e6275 )
by Ariel
14:28
created

AppointmentPresenter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
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\Duration;
7
use Timegridio\Concierge\Models\Appointment;
8
9
class AppointmentPresenter extends BasePresenter
10
{
11 9
    protected function timezone()
12
    {
13 9
        $userTimezone = auth()->user()->pref('timezone');
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...
14 9
15
        return ($userTimezone) ?: $this->wrappedObject->business->timezone;
16 1
    }
17
18 1
    public function __construct(Appointment $resource)
19
    {
20 1
        $this->wrappedObject = $resource;
21
    }
22
23 1
    public function code()
24
    {
25
        $length = $this->wrappedObject->business->pref('appointment_code_length');
26
27
        return strtoupper(substr($this->wrappedObject->hash, 0, $length));
28
    }
29
30
    public function date($format = 'Y-m-d')
31
    {
32
        // Translated text for friendly date should not be resposibility of this class
33
34
        // if ($this->wrappedObject->start_at->isToday()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35 1
        //     return studly_case(trans('Concierge::appointments.text.today'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
        // }
37 1
38 1
        // if ($this->wrappedObject->start_at->isTomorrow()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39 1
        //     return studly_case(trans('Concierge::appointments.text.tomorrow'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40 1
        // }
41
42
        $dateFormat = $this->dateFormat($format);
43 1
44
        return $this->wrappedObject
45 1
                    ->start_at
46
                    ->timezone($this->timezone())
47 1
                    ->format($dateFormat);
48 1
    }
49 1
50 1
    public function time()
51
    {
52
        $timeFormat = $this->timeFormat();
53 2
54
        return $this->wrappedObject
55 2
                    ->start_at
56
                    ->timezone($this->timezone())
57 2
                    ->format($timeFormat);
58 1
    }
59
60
    public function arriveAt()
61 1
    {
62 1
        $timeFormat = $this->timeFormat();
63 1
64 1
        if (!$this->wrappedObject->business->pref('appointment_flexible_arrival')) {
65 1
            return ['at' => $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...
66
        }
67 1
68 1
        $fromTime = $this->wrappedObject
69 1
                         ->vacancy
70 1
                         ->start_at
71 1
                         ->timezone($this->timezone())
72
                         ->format($timeFormat);
73 1
74
        $toTime = $this->wrappedObject
75
                       ->vacancy
76
                       ->finish_at
77
                       ->timezone($this->timezone())
78
                       ->format($timeFormat);
79
80
        return ['from' => $fromTime, 'to' => $toTime];
81
    }
82
83
    public function finishTime()
84
    {
85
        $timeFormat = $this->timeFormat();
86 1
87
        return $this->wrappedObject
88 1
                    ->finish_at
89
                    ->timezone($this->timezone())
90 1
                    ->format($timeFormat);
91 1
    }
92 1
93
    public function duration()
94 1
    {
95
        $duration = new Duration(intval($this->wrappedObject->duration()) * 60000);
96 1
        $format = [
97
            'template'=>'{hours} {minutes} {seconds}',
98
            '{hours}'=>'{hours} hours',
99 1
            '{minutes}'=>'{minutes} minutes',
100
            '{seconds}'=>'{seconds} seconds'
101 1
        ];
102
103
        return $duration->format($format);
104 1
    }
105
106 1
    public function phone()
107
    {
108
        return $this->wrappedObject->business->phone;
109
    }
110
111
    public function location()
112
    {
113
        return $this->wrappedObject->business->postal_address;
114
    }
115
116
    public function statusLetter()
117
    {
118
        return substr(trans('appointments.status.'.$this->wrappedObject->statusLabel), 0, 1);
119 1
    }
120
121 1
    public function status()
122 1
    {
123 1
        return trans('appointments.status.'.$this->wrappedObject->statusLabel);
124
    }
125 1
126 1
    public function statusToCssClass()
127
    {
128 1
        switch ($this->wrappedObject->status) {
129 1
            case Appointment::STATUS_CANCELED:
130
                return 'danger';
131 1
                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...
132 1
            case Appointment::STATUS_CONFIRMED:
133 1
                return 'success';
134
                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...
135
            case Appointment::STATUS_RESERVED:
136
                return 'warning';
137
                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...
138
            case Appointment::STATUS_SERVED:
139
            default:
140
                return 'default';
141
        }
142
    }
143
144
    public function panel()
145
    {
146
        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...
147 2
    }
148
149 2
    public function row()
150
    {
151
        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...
152 1
    }
153
154 1
    protected function timeFormat()
155
    {
156
        return $this->wrappedObject->business->pref('time_format') ?: 'h:i a';
157
    }
158
159
    protected function dateFormat($defaultFormat = 'Y-m-d')
160
    {
161
        return $this->wrappedObject->business->pref('date_format') ?: $defaultFormat;
162
    }
163
}
164