Completed
Push — master ( a1987d...ef66da )
by Terrence
21:32 queued 06:26
created

MyProxy::getMyProxyCredential()   B

Complexity

Conditions 11
Paths 97

Size

Total Lines 76
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 41
nc 97
nop 9
dl 0
loc 76
ccs 0
cts 60
cp 0
crap 132
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace CILogon\Service;
4
5
use CILogon\Service\Util;
6
7
define('MYPROXY_LOGON', '/usr/bin/myproxy-logon');
8
define('MYPROXY_HOST', 'myproxy.cilogon.org');
9
define('MYPROXY_PORT', '7512');
10
define('MYPROXY_LIFETIME', '12');
11
12
/**
13
 * MyProxy
14
 */
15
class MyProxy
16
{
17
    /**
18
     * getMyProxyCredential
19
     *
20
     * This function gets an X.509 credential (as a string) for a user.
21
     *
22
     * @param string $username The MyProxy user name (-l)
23
     * @param string $passphrase (Optional) The MyProxy password for the
24
     *        username (-S). Defaults to empty string.  NOTE: If $passphrase
25
     *        is non-empty, you CANNOT set a $certreq.
26
     * @param string $server (Optional) The MyProxy server to connect to (-s).
27
     *        Defaults to MYPROXY_HOST.
28
     * @param int $port (Optional) The port for the MyProxy server (-p).
29
     *        Defaults to MYPROXY_PORT.
30
     * @param int $lifetime (Optional) The life of the proxy in hours (-t).
31
     *        Defaults to MYPROXY_LIFETIME hours.
32
     * @param string $usercert (Optional) The X509_USER_CERT environment
33
     *        variable, OR the X509_USER_PROXY environment variable if
34
     *        $userkey is set to the empty string.  Defaults to empty string.
35
     * @param string $userkey (Optional) The X509_USER_KEY environment
36
     *        variable. Defaults to empty string.
37
     * @param string $certreq (Optional) A certificate request created by the
38
     *        openssl req command (--certreq).  Defaults to empty string.
39
     *        NOTE: If $certreq is non-empty, you CANNOT set a $passphrase.
40
     * @param string $env (Optional) Extra environment variables in the form
41
     *        of space-separated 'key=value' pairs.
42
     * @return string An X509 credential in a string upon success, or
43
     *         an empty string upon failure.
44
     */
45
    public static function getMyProxyCredential(
46
        $username,
47
        $passphrase = '',
48
        $server = MYPROXY_HOST,
49
        $port = MYPROXY_PORT,
50
        $lifetime = MYPROXY_LIFETIME,
51
        $usercert = '',
52
        $userkey = '',
53
        $certreq = '',
54
        $env = ''
55
    ) {
56
        $retstr = '';
57
58
        // Make sure the username passed in is not empty
59
        if (strlen($username) == 0) {
60
            Util::sendErrorAlert(
61
                'getMyProxyCredential Error',
62
                'MyProxy Error = Missing MyProxy username'
63
            );
64
            return $retstr;
65
        }
66
67
        // Don't allow weird port numbers, i.e. negative or over 65535
68
        if (($port < 0) || ($port > 65535)) {
69
            $port = MYPROXY_PORT;
70
        }
71
72
        // Don't allow weird lifetimes, i.e. negative or over 5 years
73
        if (($lifetime < 0) || ($lifetime > 43800)) {
74
            $lifetime = MYPROXY_LIFETIME;
75
        }
76
77
        // If the usercert (X509_USER_CERT) is specified, check to see if
78
        // the userkey (X509_USER_KEY) was as well.  If not, set userkey to
79
        // usercert, in effect making usercert act like X509_USER_PROXY. Then,
80
        // set the USER_CERT_ENV variable to bundle the two parameters into a
81
        // single variable holding the two X509_USER_* environment variables.
82
        $USER_CERT_ENV = '';
83
        if (strlen($usercert) > 0) {
84
            if (strlen($userkey) == 0) {
85
                $userkey = $usercert;
86
            }
87
            $USER_CERT_ENV = 'X509_USER_CERT=' . escapeshellarg($usercert) .
88
                             ' ' .
89
                             'X509_USER_KEY='  . escapeshellarg($userkey);
90
        }
91
92
        // Run the myproxy-logon command and capture the output and any error
93
        unset($output);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output seems to be never defined.
Loading history...
94
        $cmd = '/bin/env ' .
95
               $USER_CERT_ENV . ' ' .
96
               $env . ' ' .
97
               'MYPROXY_SOCKET_TIMEOUT=1 ' .
98
               MYPROXY_LOGON . ' ' .
99
               ' -s ' . escapeshellarg($server) .
100
               " -p $port" .
101
               " -t $lifetime" .
102
               ' -l ' . escapeshellarg($username) .
103
               ' -S -o -' .
104
               ((strlen($certreq) > 0) ?
105
                   (' --certreq - <<< ' . escapeshellarg($certreq)) : '') .
106
               ((strlen($passphrase) > 0) ?
107
                   (' <<< ' . escapeshellarg($passphrase)) : ' -n') .
108
               ' 2>&1';
109
        exec($cmd, $output, $return_val);
110
        $retstr = implode("\n", $output);
111
112
        if ($return_val > 0) {
113
            Util::sendErrorAlert(
114
                'getMyProxyCredential Error',
115
                "MyProxy Error = $return_val\nMyProxy Output= $retstr"
116
            );
117
            $retstr = '';
118
        }
119
120
        return $retstr;
121
    }
122
123
    /**
124
     * getDefaultLifetime
125
     *
126
     * This function returns the value of the class defined
127
     * MYPROXY_LIFETIME as an int, which may be needed in '/secure/getuser'
128
     * when getting a certificate.
129
     *
130
     * @return int The value of MYPROXY_LIFETIME
131
     */
132
    public static function getDefaultLifetime()
133
    {
134
        return (int)MYPROXY_LIFETIME;
135
    }
136
}
137