Completed
Pull Request — master (#537)
by Daniel
04:10
created

DateDifference   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A format() 0 7 2
A diffForHumans() 0 13 4
1
<?php
2
3
namespace Coyote\Services\Helper;
4
5
use Carbon\Carbon;
6
7
class DateDifference
8
{
9
    /** @var string */
10
    private $format;
11
    /** @var bool */
12
    private $diffForHumans;
13
14
    public function __construct(string $format, bool $diffForHumans)
15
    {
16
        $this->format = $format;
17
        $this->diffForHumans = $diffForHumans;
18
    }
19
20
    public function format(Carbon $earlier): string
21
    {
22
        if ($this->diffForHumans) {
23
            return $this->diffForHumans($earlier);
24
        }
25
        return $earlier->formatLocalized($this->format);
26
    }
27
28
    private function diffForHumans(Carbon $earlier): string
29
    {
30
        if ($earlier->diffInHours() < 1) {
31
            return $earlier->diffForHumans(null, true) . ' temu';
32
        }
33
        if ($earlier->isToday()) {
34
            return 'dziś, ' . $earlier->format('H:i');
35
        }
36
        if ($earlier->isYesterday()) {
37
            return 'wczoraj, ' . $earlier->format('H:i');
38
        }
39
        return $earlier->formatLocalized($this->format);
40
    }
41
}
42