Mcrypt   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 27
wmc 5
lcom 0
cbo 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 14 4
A isAvailable() 0 4 1
1
<?php
2
/**
3
 * Caridea
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
 * use this file except in compliance with the License. You may obtain a copy of
7
 * the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
 * License for the specific language governing permissions and limitations under
15
 * the License.
16
 *
17
 * @copyright 2015-2016 LibreWorks contributors
18
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
19
 */
20
namespace Caridea\Random;
21
22
/**
23
 * A provider of cryptographically-strong random bytes using Mcrypt.
24
 *
25
 * @copyright 2015-2016 LibreWorks contributors
26
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
27
 */
28
class Mcrypt implements Generator
29
{
30
    public function generate($length)
31
    {
32
        $bytes = '';
33
        $length = (int)$length;
34
        
35
        if (self::isAvailable() && $length > 0) {
36
            $mcrypt = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
37
            if ($mcrypt !== false) {
38
                $bytes = $mcrypt;
39
            }
40
        }
41
        
42
        return str_pad($bytes, $length, chr(0));
43
    }
44
    
45
    /**
46
     * Determines whether this provider can be run (e.g. all needed PHP extensions are available).
47
     *
48
     * @return bool Whether this provider is available
49
     */
50
    public static function isAvailable()
51
    {
52
        return function_exists('mcrypt_create_iv');
53
    }
54
}
55