Skip to content

Commit d56749c

Browse files
committed
Refactor in JasperPHP::execute() and create a Exceptions
1 parent d3c86ca commit d56749c

File tree

5 files changed

+120
-12
lines changed

5 files changed

+120
-12
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace JasperPHP\Exception;
3+
/**
4+
* Class ErrorCommandExecutable
5+
* @package JasperPHP\Exception
6+
*/
7+
class ErrorCommandExecutable extends \Exception
8+
{
9+
public function __construct($message = "", $code = 0, Exception $previous = null)
10+
{
11+
$message = 'Your report has an error and couldn \'t be processed!\ Try to output the command using the function `output();` and run it manually in the console.';
12+
parent::__construct($message, $code, $previous);
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace JasperPHP\Exception;
3+
/**
4+
* Class InvalidCommandExecutable
5+
* @package JasperPHP\Exception
6+
*/
7+
class InvalidCommandExecutable extends \Exception
8+
{
9+
10+
/**
11+
* InvalidCommandExecutable constructor.
12+
* @param string $message
13+
* @param int $code
14+
* @param Exception|null $previous
15+
*/
16+
public function __construct($message = "", $code = 0, Exception $previous = null)
17+
{
18+
$message = 'Cannot execute a blank command';
19+
parent::__construct($message, $code, $previous);
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
namespace JasperPHP\Exception;
3+
/**
4+
* Class InvalidResourceDirectory
5+
* @package JasperPHP\Exception
6+
*/
7+
class InvalidResourceDirectory extends \Exception
8+
{
9+
10+
/**
11+
* Invalid Resource Directory constructor.
12+
*
13+
* @param string $message
14+
* @param int $code
15+
* @param Exception|null $previous
16+
*/
17+
public function __construct($message = "", $code = 0, Exception $previous = null)
18+
{
19+
$message = 'Invalid resource directory';
20+
parent::__construct($message, $code, $previous);
21+
}
22+
}

src/JasperPHP.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
namespace JasperPHP;
3+
34
/**
45
* Class JasperPHP
56
*
@@ -192,26 +193,54 @@ public function output()
192193
return $this->the_command;
193194
}
194195

195-
public function execute($run_as_user = false)
196+
/**
197+
* @param bool $user
198+
* @return mixed
199+
* @throws Exception\InvalidCommandExecutable
200+
* @throws Exception\InvalidResourceDirectory
201+
* @throws Exception\ErrorCommandExecutable
202+
*/
203+
public function execute($user = false)
196204
{
197-
if ($run_as_user !== false && strlen($run_as_user > 0) && !$this->windows) {
198-
$this->the_command = 'su -u ' . $run_as_user . " -c \"" . $this->the_command . "\"";
199-
}
205+
$this->validateExecute();
206+
$this->addUserToCommand($user);
200207

208+
$command = $this->the_command;
201209
$output = [];
202210
$return_var = 0;
203211

204-
if (is_dir($this->path_executable)) {
205-
chdir($this->path_executable);
206-
exec($this->the_command, $output, $return_var);
207-
} else {
208-
throw new \Exception('Invalid resource directory.', 1);
212+
chdir($this->path_executable);
213+
exec($command, $output, $return_var);
214+
if ($return_var !== 0) {
215+
throw new \JasperPHP\Exception\ErrorCommandExecutable();
216+
}
217+
218+
return $output;
219+
}
220+
221+
/**
222+
* @param $user
223+
*/
224+
protected function addUserToCommand($user)
225+
{
226+
if ($user && !$this->windows) {
227+
$this->the_command = 'su -u ' . $user . " -c \"" . $this->the_command . "\"";
209228
}
229+
}
210230

211-
if ($return_var != 0) {
212-
throw new \Exception('Your report has an error and couldn \'t be processed!\ Try to output the command using the function `output();` and run it manually in the console.', 1);
231+
/**
232+
* @throws Exception\InvalidCommandExecutable
233+
* @throws Exception\InvalidResourceDirectory
234+
*/
235+
protected function validateExecute()
236+
{
237+
if (!$this->the_command) {
238+
throw new \JasperPHP\Exception\InvalidCommandExecutable();
239+
}
240+
if (!is_dir ($this->path_executable)) {
241+
throw new \JasperPHP\Exception\InvalidResourceDirectory();
213242
}
214243

215-
return $output;
216244
}
245+
217246
}

tests/JasperPHP/JasperPHPTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,26 @@ public function testConstructor()
1717
$this->assertInstanceOf(JasperPHP::class, new JasperPHP());
1818
}
1919

20+
/**
21+
*
22+
*/
23+
public function testExecuteWithoutCompile()
24+
{
25+
$this->setExpectedException(\JasperPHP\Exception\InvalidCommandExecutable::class);
26+
27+
$jasper = new JasperPHP();
28+
$jasper->execute();
29+
}
30+
31+
/**
32+
*
33+
*/
34+
public function testExecuteWithCompileAndWrongInput()
35+
{
36+
$this->setExpectedException(\JasperPHP\Exception\ErrorCommandExecutable::class);
37+
38+
$jasper = new JasperPHP();
39+
$jasper->compile('hello_world.jrxml')->execute();
40+
}
41+
2042
}

0 commit comments

Comments
 (0)