Completed
Pull Request — master (#1105)
by Tim
42:55
created

GeneratorThread::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * \AppserverIo\Appserver\Core\GeneratorThread
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the Open Software License (OSL 3.0)
8
 * that is available through the world-wide-web at this URL:
9
 * http://opensource.org/licenses/osl-3.0.php
10
 *
11
 * PHP version 5
12
 *
13
 * @author    Bernhard Wick <[email protected]>
14
 * @copyright 2015 TechDivision GmbH - <[email protected]>
15
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
16
 * @link      https://github.com/appserver-io/appserver
17
 * @link      http://www.appserver.io
18
 */
19
20
namespace AppserverIo\Appserver\Core;
21
22
use AppserverIo\Doppelgaenger\Generator;
23
use Psr\Log\LogLevel;
24
25
/**
26
 * Simple thread for parallel creation of contract-enabled structure definitions.
27
 *
28
 * @author    Bernhard Wick <[email protected]>
29
 * @copyright 2015 TechDivision GmbH - <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/appserver
32
 * @link      http://www.appserver.io
33
 */
34
class GeneratorThread extends \Thread
35
{
36
37
    /**
38
     * Generator instance to use for creation
39
     *
40
     * @var \AppserverIo\Doppelgaenger\Generator $generator
41
     */
42
    protected $generator;
43
44
    /**
45
     * Array of structures we will be creating
46
     *
47
     * @var array<\AppserverIo\Doppelgaenger\Entities\Definitions\Structure> $structures
48
     */
49
    protected $structures;
50
51
    /**
52
     * Default constructor
53
     *
54
     * @param \AppserverIo\Doppelgaenger\Generator $generator  Our Doppelgaenger generator instance
55
     * @param array                                $structures List of structures to generate
56
     */
57
    public function __construct(Generator $generator, array $structures)
58
    {
59
        $this->generator = $generator;
60
        $this->structures = $structures;
61
    }
62
63
    /**
64
     * Run method
65
     *
66
     * @return void
67
     */
68
    public function run()
69
    {
70
71
        // register a shutdown function
72
        register_shutdown_function(array($this, 'shutdown'));
73
74
        // register the default autoloader
75
        require SERVER_AUTOLOADER;
76
77
        try {
78
            // iterate over all structures and generate them
79
            foreach ($this->structures as $structure) {
80
                $this->generator->create($structure);
81
            }
82
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
83
        } catch (\Exception $e) {
84
            $this->log(LogLevel::ERROR, $e->__toString());
85
        }
86
    }
87
88
    /**
89
     * The shutdown method implementation.
90
     *
91
     *@return void
92
     */
93
    public function shutdown()
94
    {
95
96
        // check if there was a fatal error caused shutdown
97
        if ($lastError = error_get_last()) {
98
            // initialize error type and message
99
            $type = 0;
100
            $message = '';
101
            // extract the last error values
102
            extract($lastError);
103
            // query whether we've a fatal/user error
104
            if ($type === E_ERROR || $type === E_USER_ERROR) {
105
                $this->log(LogLevel::ERROR, $message);
106
            }
107
        }
108
    }
109
110
    /**
111
     * This is a very basic method to log some stuff by using the error_log() method of PHP.
112
     *
113
     * @param mixed  $level   The log level to use
114
     * @param string $message The message we want to log
115
     * @param array  $context The context we of the message
116
     *
117
     * @return void
118
     */
119
    public function log($level, $message, array $context = array())
120
    {
121
        error_log(sprintf($this->getDefaultLogFormat(), date('Y-m-d H:i:s'), gethostname(), $level, $message, json_encode($context)));
0 ignored issues
show
Bug introduced by
The method getDefaultLogFormat() does not exist on AppserverIo\Appserver\Core\GeneratorThread. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        error_log(sprintf($this->/** @scrutinizer ignore-call */ getDefaultLogFormat(), date('Y-m-d H:i:s'), gethostname(), $level, $message, json_encode($context)));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
    }
123
}
124