Completed
Push — master ( 88271e...c5e0e1 )
by Ariel
12:08
created

AppointmentPresenter::dateFormat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
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
    protected $timezone;
12
13 9
    public function timezone($timezone = false)
14
    {
15 9
        $this->timezone = $timezone ?: $this->wrappedObject->business->timezone;
16
17 9
        return $this;
18
    }
19
20 9
    public function __construct(Appointment $resource)
21
    {
22 9
        $this->wrappedObject = $resource;
23
24 9
        $this->timezone(session()->get('timezone'));
25 9
    }
26
27 1
    public function code()
28
    {
29 1
        $length = $this->wrappedObject->business->pref('appointment_code_length');
30
31 1
        return strtoupper(substr($this->wrappedObject->hash, 0, $length));
32
    }
33
34 1
    public function date($format = 'Y-m-d')
35
    {
36
        // Translated text for friendly date should not be resposibility of this class
37
38
        // 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...
39
        //     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...
40
        // }
41
42
        // 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...
43
        //     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...
44
        // }
45
46 1
        $dateFormat = $this->dateFormat($format);
47
48 1
        return $this->wrappedObject
49 1
                    ->start_at
50 1
                    ->timezone($this->timezone)
51 1
                    ->format($dateFormat);
52
    }
53
54 1
    public function time()
55
    {
56 1
        $timeFormat = $this->timeFormat();
57
58 1
        return $this->wrappedObject
59 1
                    ->start_at
60 1
                    ->timezone($this->timezone)
61 1
                    ->format($timeFormat);
62
    }
63
64 2
    public function arriveAt()
65
    {
66 2
        $timeFormat = $this->timeFormat();
67
68 2
        if (!$this->wrappedObject->business->pref('appointment_flexible_arrival')) {
69 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...
70
        }
71
72 1
        $fromTime = $this->wrappedObject
73 1
                         ->vacancy
74 1
                         ->start_at
75 1
                         ->timezone($this->timezone)
76 1
                         ->format($timeFormat);
77
78 1
        $toTime = $this->wrappedObject
79 1
                       ->vacancy
80 1
                       ->finish_at
81 1
                       ->timezone($this->timezone)
82 1
                       ->format($timeFormat);
83
84 1
        return ['from' => $fromTime, 'to' => $toTime];
85
    }
86
87
    public function finishTime()
88
    {
89
        $timeFormat = $this->timeFormat();
90
91
        return $this->wrappedObject
92
                    ->finish_at
93
                    ->timezone($this->timezone)
94
                    ->format($timeFormat);
95
    }
96
97 1
    public function duration()
98
    {
99 1
        $duration = new Duration(intval($this->wrappedObject->duration()) * 60000);
100
        $format = [
101 1
            'template' => '{hours} {minutes} {seconds}',
102 1
            '{hours}'  => '{hours} hours',
103 1
            '{minutes}' => '{minutes} minutes',
104 1
            '{seconds}' => '{seconds} seconds',
105 1
        ];
106
107 1
        return $duration->format($format);
108
    }
109
110 1
    public function phone()
111
    {
112 1
        return $this->wrappedObject->business->phone;
113
    }
114
115 1
    public function location()
116
    {
117 1
        return $this->wrappedObject->business->postal_address;
118
    }
119
120
    public function statusLetter()
121
    {
122
        return substr(trans('appointments.status.'.$this->wrappedObject->statusLabel), 0, 1);
123
    }
124
125
    public function status()
126
    {
127
        return trans('appointments.status.'.$this->wrappedObject->statusLabel);
128
    }
129
130 1
    public function statusToCssClass()
131
    {
132 1
        switch ($this->wrappedObject->status) {
133 1
            case Appointment::STATUS_CANCELED:
134 1
                return 'danger';
135
                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...
136 1
            case Appointment::STATUS_CONFIRMED:
137 1
                return 'success';
138
                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...
139 1
            case Appointment::STATUS_RESERVED:
140 1
                return 'warning';
141
                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...
142 1
            case Appointment::STATUS_SERVED:
143 1
            default:
144 1
                return 'default';
145
        }
146
    }
147
148
    public function panel()
149
    {
150
        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...
151
    }
152
153
    public function row()
154
    {
155
        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...
156
    }
157
158 2
    protected function timeFormat()
159
    {
160 2
        return $this->wrappedObject->business->pref('time_format') ?: 'h:i a';
161
    }
162
163 1
    protected function dateFormat($defaultFormat = 'Y-m-d')
164
    {
165 1
        return $this->wrappedObject->business->pref('date_format') ?: $defaultFormat;
166
    }
167
}
168