2013-07-13 00:19:43 +02:00
< ? php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin ;
2013-07-13 00:19:43 +02:00
2016-07-19 20:28:11 +02:00
use PHPCensor\Builder ;
use PHPCensor\Model\Build ;
use PHPCensor\Plugin ;
2013-10-10 02:01:06 +02:00
2013-07-13 00:19:43 +02:00
/**
* Pdepend Plugin - Allows Pdepend report
2018-01-20 07:09:30 +01:00
*
2017-03-04 16:39:56 +01:00
* @ author Johan van der Heide < info @ japaveh . nl >
2013-07-13 00:19:43 +02:00
*/
2016-07-11 18:00:04 +02:00
class Pdepend extends Plugin
2013-07-13 00:19:43 +02:00
{
protected $args ;
2016-07-11 18:00:04 +02:00
2018-01-20 07:09:30 +01:00
/**
* @ var string
*/
protected $buildDirectory ;
2013-07-13 00:19:43 +02:00
/**
* @ var string Directory which needs to be scanned
*/
protected $directory ;
2016-07-11 18:00:04 +02:00
2013-07-13 00:19:43 +02:00
/**
* @ var string File where the summary . xml is stored
*/
protected $summary ;
2016-07-11 18:00:04 +02:00
2013-07-13 00:19:43 +02:00
/**
* @ var string File where the chart . svg is stored
*/
protected $chart ;
2016-07-11 18:00:04 +02:00
2013-07-13 00:19:43 +02:00
/**
* @ var string File where the pyramid . svg is stored
*/
protected $pyramid ;
2016-07-11 18:00:04 +02:00
2013-07-13 00:19:43 +02:00
/**
* @ var string Location on the server where the files are stored . Preferably in the webroot for inclusion
* in the readme . md of the repository
*/
protected $location ;
2017-01-11 16:15:54 +01:00
/**
* @ return string
*/
public static function pluginName ()
{
return 'pdepend' ;
}
2014-12-08 12:25:33 +01:00
/**
2016-07-11 18:00:04 +02:00
* { @ inheritdoc }
2014-12-08 12:25:33 +01:00
*/
2016-07-22 09:05:34 +02:00
public function __construct ( Builder $builder , Build $build , array $options = [])
2013-07-13 00:19:43 +02:00
{
2016-07-22 09:05:34 +02:00
parent :: __construct ( $builder , $build , $options );
2013-07-13 00:19:43 +02:00
2018-01-20 07:09:30 +01:00
$this -> directory = isset ( $options [ 'directory' ])
? $options [ 'directory' ]
: $this -> builder -> buildPath ;
$this -> summary = 'summary.xml' ;
$this -> pyramid = 'pyramid.svg' ;
$this -> chart = 'chart.svg' ;
2013-07-13 00:19:43 +02:00
2018-01-20 07:09:30 +01:00
$this -> buildDirectory = $build -> getProjectId () . '/' . $build -> getId ();
$this -> location = PUBLIC_DIR . 'artifacts/pdepend/' . $this -> buildDirectory ;
2013-07-13 00:19:43 +02:00
}
/**
* Runs Pdepend with the given criteria as arguments
*/
public function execute ()
{
2015-05-29 08:26:47 +02:00
if ( ! file_exists ( $this -> location )) {
2018-01-20 07:09:30 +01:00
mkdir ( $this -> location , 0777 , true );
2015-05-29 08:26:47 +02:00
}
2013-07-13 00:19:43 +02:00
if ( ! is_writable ( $this -> location )) {
2015-05-29 08:26:47 +02:00
throw new \Exception ( sprintf ( 'The location %s is not writable or does not exist.' , $this -> location ));
2013-07-13 00:19:43 +02:00
}
2017-07-19 05:26:51 +02:00
$pdepend = $this -> findBinary ( 'pdepend' );
2013-10-08 09:50:10 +02:00
$cmd = $pdepend . ' --summary-xml="%s" --jdepend-chart="%s" --overview-pyramid="%s" %s "%s"' ;
2013-07-13 00:19:43 +02:00
2013-08-10 03:34:19 +02:00
$this -> removeBuildArtifacts ();
2015-04-14 12:17:52 +02:00
2013-08-10 03:28:51 +02:00
// If we need to ignore directories
2016-07-22 09:05:34 +02:00
if ( count ( $this -> builder -> ignore )) {
$ignore = ' --ignore=' . implode ( ',' , $this -> builder -> ignore );
2013-08-10 03:28:51 +02:00
} else {
$ignore = '' ;
}
2016-07-22 09:05:34 +02:00
$success = $this -> builder -> executeCommand (
2013-07-13 00:19:43 +02:00
$cmd ,
$this -> location . DIRECTORY_SEPARATOR . $this -> summary ,
$this -> location . DIRECTORY_SEPARATOR . $this -> chart ,
$this -> location . DIRECTORY_SEPARATOR . $this -> pyramid ,
2013-08-10 03:28:51 +02:00
$ignore ,
2013-07-13 00:19:43 +02:00
$this -> directory
);
2016-07-22 09:05:34 +02:00
$config = $this -> builder -> getSystemConfig ( 'php-censor' );
2013-07-13 00:19:43 +02:00
if ( $success ) {
2016-07-22 09:05:34 +02:00
$this -> builder -> logSuccess (
2013-07-13 00:19:43 +02:00
sprintf (
2018-01-20 07:09:30 +01:00
" \n Pdepend successful. \n You can use: %s, \n ![Chart](%s \" Pdepend Chart \" ) and \n ![Pyramid](%s \" Pdepend Pyramid \" ) \n for inclusion in the readme.md file " ,
$config [ 'url' ] . '/artifacts/pdepend/' . $this -> buildDirectory . '/' . $this -> summary ,
$config [ 'url' ] . '/artifacts/pdepend/' . $this -> buildDirectory . '/' . $this -> chart ,
$config [ 'url' ] . '/artifacts/pdepend/' . $this -> buildDirectory . '/' . $this -> pyramid
2013-07-13 00:19:43 +02:00
)
);
}
return $success ;
}
2013-08-10 03:34:19 +02:00
/**
* Remove files created from previous builds
*/
protected function removeBuildArtifacts ()
{
//Remove the created files first
2016-04-21 06:58:09 +02:00
foreach ([ $this -> summary , $this -> chart , $this -> pyramid ] as $file ) {
2013-08-10 03:34:19 +02:00
if ( file_exists ( $this -> location . DIRECTORY_SEPARATOR . $file )) {
unlink ( $this -> location . DIRECTORY_SEPARATOR . $file );
}
}
}
}