Completed
Branch feature/phpstanLevel2 (e9b6b0)
by Schlaefer
02:36
created

LastRefreshAbstract::_parseTimestamp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
LastRefreshAbstract::_set() 0 1 ?
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\User\LastRefresh;
14
15
use Saito\User\CurrentUser\CurrentUserInterface;
16
17
/**
18
 * handles last refresh time for the current user
19
 */
20
abstract class LastRefreshAbstract
21
{
22
23
    /**
24
     * @var CurrentUserInterface
25
     */
26
    protected $_CurrentUser;
27
28
    /**
29
     * @var \DateTimeInterface timestamp
30
     */
31
    protected $_timestamp = null;
32
33
    /**
34
     * @var mixed storage object depending on implementation
35
     */
36
    protected $_storage;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param CurrentUserInterface $CurrentUser current-user
42
     * @param mixed $storage storage a storage handler
43
     */
44
    public function __construct(CurrentUserInterface $CurrentUser, $storage = null)
45
    {
46
        $this->_CurrentUser = $CurrentUser;
47
        $this->_storage = $storage;
48
    }
49
50
    /**
51
     * Checks if last refresh newer than $timestamp.
52
     *
53
     * Performance sensitive: every posting on /entries/index may be
54
     * tested if new for user.
55
     *
56
     * @param string|\DateTimeInterface $timestamp int unix-timestamp or date as string
57
     * @return mixed bool or null if not determinable
58
     */
59
    public function isNewerThan($timestamp)
60
    {
61
        $lastRefresh = $this->_get();
62
        // timestamp is not set (or readable): everything is considered new
63
        if ($lastRefresh === false) {
64
            return null;
65
        }
66
67
        return $lastRefresh > dateToUnix($timestamp);
68
    }
69
70
    /**
71
     * returns last refresh timestamp
72
     *
73
     * @return mixed int if unix timestamp or bool false if uninitialized
74
     */
75
    abstract protected function _get();
76
77
    /**
78
     * Mark last refresh as "now"
79
     *
80
     * @return void
81
     */
82
    public function set(): void
83
    {
84
        $this->_timestamp = new \DateTimeImmutable();
85
        $this->_set();
86
87
        // All postings indiviually marked as read are now older than the
88
        // last-refresh timestamp and can be removed.
89
        $this->_CurrentUser->ReadEntries->delete();
0 ignored issues
show
Bug introduced by
Accessing ReadEntries on the interface Saito\User\CurrentUser\CurrentUserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
90
    }
91
92
    /**
93
     * Set timestamp implementation
94
     *
95
     * @return void
96
     */
97
    abstract protected function _set();
98
}
99