ContentParser::formattedDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Utils;
4
5
use App\Models\Candidate;
6
use Carbon\Carbon;
7
8
class ContentParser
9
{
10
    public static function parse(string $content, Candidate $candidate, \DateTime $appointmentDate = null, $user = null)
11
    {
12
        $mapping = [
13
            '%%JOB_TITLE%%'                => $candidate->recruitment->job_title,
14
            '%%CANDIDATE_NAME%%'           => $candidate->name,
15
            '%%USER_FIRST_NAME%%'          => $user ? $user->name : '',
16
            '%%USER_LAST_NAME%%'           => '',
17
            '%%APPOINTMENT_NATURAL_DATE%%' => $appointmentDate ? self::naturalDate($appointmentDate) : '',
18
            '%%APPOINTMENT_DATE%%'         => $appointmentDate ? self::formattedDate($appointmentDate) : '',
19
            '%%APPOINTMENT_TIME%%'         => $appointmentDate ? $appointmentDate->format('G:i') : '',
20
        ];
21
22
        foreach ($mapping as $variable => $value) {
23
            $content = str_replace($variable, $value, $content);
24
        }
25
26
        return $content;
27
    }
28
29
    protected static function naturalDate($date)
30
    {
31
        $prefix = '';
32
        $carbonDate = Carbon::instance($date)->locale('pl_PL');
33
        $carbonDateYesterday = $carbonDate->copy();
34
35
        if ($carbonDate->isTomorrow()) {
36
            $prefix = 'jutro, tj. ';
37
        } elseif ($carbonDateYesterday->subDay(1)->isTomorrow()) {
0 ignored issues
show
Unused Code introduced by
The call to Carbon\Carbon::subDay() has too many arguments starting with 1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        } elseif ($carbonDateYesterday->/** @scrutinizer ignore-call */ subDay(1)->isTomorrow()) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
38
            $prefix = 'pojutrze, tj. ';
39
        }
40
41
        return $prefix.$carbonDate->isoFormat('dddd, D MMMM');
42
    }
43
44
    protected static function formattedDate($date)
45
    {
46
        $carbonDate = Carbon::instance($date)->locale('pl_PL');
47
48
        return $carbonDate->isoFormat('dddd, D MMMM');
49
    }
50
}
51