Comment::timeElapsedString()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 8
nop 1
crap 5
1
<?php
2
3
namespace Emsa\Comment;
4
5
use \LRC\Form\BaseModel;
6
use \LRC\Repository\SoftManagedModelInterface;
7
use \LRC\Repository\SoftManagedModelTrait;
8
use \Emsa\User\User;
9
10
/**
11
 * Comment class.
12
 *
13
 * @SuppressWarnings(PHPMD.ExitExpression)
14
 * @SuppressWarnings(PHPMD.CamelCasePropertyName)
15
 */
16
class Comment extends BaseModel implements SoftManagedModelInterface
17
{
18
    use SoftManagedModelTrait;
19
20
    // public $id;
21
    public $post_id;
22
    public $parent_id;
23
    public $user;
24
    // public $created;
25
    // public $edited;
26
    public $content;
27
    // public $upvote;
28
    // public $downvote;
29
    // public $deleted;
30
31
32
33 6
    public function __construct()
34
    {
35 6
        $this->setReferences([
36 6
            'userObject' => [
37
                'attribute' => 'user',
38
                'model' => User::class,
39
                'magic' => true
40
            ]
41
        ]);
42
43
        // $this->setNullables(['edited']);
44 6
        $this->setValidation([
45 6
            'content' => [
46
                [
47
                    'rule' => 'required',
48
                    'message' => 'Kommentaren kan inte vara tom.'
49
                ],
50
            ]
51
        ]);
52 6
    }
53
54
55
56 1
    public function timeElapsedString($datetime)
57
    {
58 1
        $now = new \DateTime;
59 1
        $ago = new \DateTime($datetime);
60 1
        $diff = $now->diff($ago);
61 1
        $diff->w = floor($diff->d / 7);
0 ignored issues
show
Bug introduced by
The property w does not seem to exist in DateInterval.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
63
        $timeValues = array(
64 1
            'y' => ['år', 'år'],
65
            'm' => ['månad', 'månader'],
66
            'w' => ['vecka', 'veckor'],
67
            'd' => ['dag', 'dagar'],
68
            'h' => ['timme', 'timmar'],
69
            'i' => ['minut', 'minuter'],
70
            's' => ['sekund', 'sekunder'],
71
        );
72
73 1
        foreach ($timeValues as $k => &$v) {
74 1
            if ($diff->$k != 0) {
75 1
                $singPlur = $diff->$k > 1 ? 1 : 0;
76 1
                $string = $diff->$k . ' ' . $v[$singPlur] . ' sedan';
77 1
                break;
78
            }
79
        }
80
81 1
        return isset($string) ? $string : 'nyligen';
82
    }
83
}
84