Completed
Pull Request — 0.3.x (#27)
by
unknown
03:41
created

LogoutManager::isHostUp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.6666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3.0067
1
<?php
2
3
namespace Krtv\Bundle\SingleSignOnIdentityProviderBundle\Manager;
4
5
use Symfony\Component\HttpFoundation\Session\SessionInterface;
6
use Symfony\Component\Routing\RouterInterface;
7
8
/**
9
 * Class LogoutManager
10
 * @package Krtv\Bundle\SingleSignOnIdentityProviderBundle\Manager
11
 */
12
class LogoutManager
13
{
14
    const SERVICE_SESSION_NS = '_logout/processed';
15
16
    /**
17
     * @var RouterInterface
18
     */
19
    private $router;
20
21
    /**
22
     * @var SessionInterface
23
     */
24
    private $session;
25
26
    /**
27
     * @var ServiceManager
28
     */
29
    private $serviceManager;
30
31
    /**
32
     * @param ServiceManager $serviceManager
33
     * @param SessionInterface $session
34
     * @param RouterInterface $router
35
     */
36 4
    public function __construct(ServiceManager $serviceManager, SessionInterface $session, RouterInterface $router)
37
    {
38 4
        $this->serviceManager = $serviceManager;
39 4
        $this->session = $session;
40 4
        $this->router = $router;
41 4
    }
42
43
    /**
44
     * @return string
45
     */
46 6
    public function getNextLogoutUrl()
47
    {
48 6
        $referrerService = $this->serviceManager->getRequestService();
49
50 6
        if ($referrerService !== null) {
51 5
            $this->addProcessedService($referrerService);
52
        }
53
54 6
        $availableServices = $this->serviceManager->getServices();
55 6
        $completedServices = $this->getCompletedServices();
56
        
57 6
        if (!empty($completedServices)){
58 5
            $first = array_values($completedServices)[0];
59 5
            $indexUrl = $this->serviceManager->getServiceManager($first)->getServiceIndexUrl();
60 4
            $this->session->set('_security.main.target_path', $indexUrl);
61
        }
62
        
63 5
        $nextService = null;
64 5
        foreach ($availableServices as $service) {
65 5
            $serviceManager = $this->serviceManager->getServiceManager($service);
66 4
            $indexUrl = $serviceManager->getServiceIndexUrl();
67
        
68 4
            if ( in_array($service, $completedServices) || !$this->isHostUp($indexUrl) ){
69 3
                continue;
70
            }
71
72 4
            $nextService = $service;
73
74 4
            break;
75
        }
76
77 4
        if ($nextService !== null) {
78 4
            $serviceManager = $this->serviceManager->getServiceManager($nextService);
79
80 3
            return $serviceManager->getServiceLogoutUrl();
81
        }
82
83 3
        return $this->router->generate('_security_logout', array(), RouterInterface::ABSOLUTE_URL);
84
    }
85
86
    /**
87
     * @param string $service
88
     */
89 5
    private function addProcessedService($service)
90
    {
91 5
        $services = $this->getCompletedServices();
92 5
        $services[$service] = $service;
93
94 5
        $this->session->set($this->getSessionKey(), $services);
95 5
    }
96
97
    /**
98
     * @return mixed
99
     */
100 6
    private function getCompletedServices()
101
    {
102 6
        return $this->session->get($this->getSessionKey(), array());
103
    }
104
105
    /**
106
     * @return string
107
     */
108 6
    private function getSessionKey()
109
    {
110 6
        return static::SERVICE_SESSION_NS;
111
    }
112
    
113
    
114 4
    public function isHostUp($url) {
115 4
        $handle = curl_init($url);
116 4
        curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
117 4
        curl_setopt($handle, CURLOPT_TIMEOUT, 5);
118 4
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
119
        
120 4
        $response = curl_exec($handle);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
121
122
        /* Check for 404 (file not found). */
123 4
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
124
        
125
        
126 4
        curl_close($handle);
127 4
        if($httpCode == 404 || $httpCode == 0) {
128
            return false;
129
        }
130 4
        return true;
131
    }
132
}
133