GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 676741...adbf35 )
by Carsten
14:06 queued 16s
created

setCacheLifeTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Germania\IpstackClient;
3
4
use Germania\IpstackClient\IpstackClientInterface;
5
use Psr\Cache\CacheItemPoolInterface;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\NullLogger;
8
use Psr\Log\LoggerAwareTrait;
9
10
class IpstackClientPsr6CacheDecorator implements IpstackClientInterface
11
{
12
	use LoggerAwareTrait;
13
14
15
	/**
16
	 * @var CacheItemPoolInterface
17
	 */
18
	public $cache;
19
20
	/**
21
	 * @var int|null
22
	 */
23
	public $cache_lifetime;
24
25
26
	/**
27
	 * @param IpstackClientInterface $ipstack_client
28
	 * @param CacheItemPoolInterface $cache           PSR-6 Cache ItemPool
29
	 */
30 24
	public function __construct( IpstackClientInterface $ipstack_client, CacheItemPoolInterface $cache, LoggerInterface $logger = null )
31
	{
32 24
		$this->ipstack_client = $ipstack_client;
0 ignored issues
show
Bug introduced by
The property ipstack_client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33 24
		$this->cache = $cache;
34 24
		$this->setLogger( $logger ?: new NullLogger );
35 24
	}
36
37
38
	/**
39
	 * @param int|null $lifetime Cache Lifetime in seconds (or NULL)
40
	 * @return self
41
	 */
42 8
	public function setCacheLifeTime( $lifetime )
43
	{
44 8
		$this->cache_lifetime = $lifetime;
45 8
		return $this;
46
	}
47
48
	/**
49
	 * @return int|null
50
	 */
51 24
	public function getCacheLifeTime()
52
	{
53 24
		return $this->cache_lifetime;
54
	}
55
56
57 16
	public function get( string $client_ip, array $custom_query = array() ) : array
58
    {
59
60 16
        $item = $this->cache->getItem( sha1($client_ip) );
61
62 16
        $ipstack = $item->get();
63
64
        // No valid item?
65 16
        if(!$item->isHit()):
66 16
            $this->logger->debug("No ipstack info in cache");
67
68
            // Run intensive code: Ask ipstack directly
69 16
            $ipstack = $this->ipstack_client->get( $client_ip, $custom_query );
70
71
            // Store data for future use.
72 16
            $this->logger->info("Write ipstack info to cache", $ipstack);
73 16
            $item->set($ipstack);
74
            
75 16
            $lifetime = $this->getCacheLifeTime();
76 16
          	$item->expiresAfter($lifetime);
77
78 16
            $this->cache->save($item);
79
80
        // Valid item found
81
        else:
82
            $this->logger->notice("Found ipstack info in cache", $ipstack);
83
        endif;
84
85 16
        return $ipstack;
86
87
	}	
88
89
}