Skip to content

Commit

Permalink
code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarzin committed May 1, 2023
1 parent 690e4a4 commit b5ae591
Show file tree
Hide file tree
Showing 19 changed files with 733 additions and 680 deletions.
6 changes: 1 addition & 5 deletions app/CPEProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace App;

use App\CPEVersion;
use App\CPEVendor;

use Illuminate\Database\Eloquent\Model;

/**
* App\CPEProduct
*
*/
class CPEProduct extends Model
{
Expand All @@ -25,7 +21,7 @@ class CPEProduct extends Model

protected $fillable = [
'cpe_vendor_id',
'name'
'name',
];

public function versions()
Expand Down
8 changes: 2 additions & 6 deletions app/CPEVendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

namespace App;

use App\CPEProduct;

use Illuminate\Database\Eloquent\Model;

/**
* App\CPEVendor
*
*/
class CPEVendor extends Model
{
public $table = 'cpe_vendors';

public $timestamps = false;

public static $searchable = [
Expand All @@ -24,12 +21,11 @@ class CPEVendor extends Model

protected $fillable = [
'part',
'name'
'name',
];

public function products()
{
return $this->belongsToMany(CPEProduct::class)->orderBy('name');
}

}
5 changes: 1 addition & 4 deletions app/CPEVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace App;

use App\CPEProduct;

use Illuminate\Database\Eloquent\Model;

/**
* App\CPEProduct
*
*/
class CPEVersion extends Model
{
Expand All @@ -24,7 +21,7 @@ class CPEVersion extends Model

protected $fillable = [
'cpe_product_id',
'name'
'name',
];

public function product()
Expand Down
51 changes: 23 additions & 28 deletions app/Console/Commands/CPEImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace App\Console\Commands;

use App\CPEVendor;
use App\CPEProduct;
use App\CPEVendor;
use App\CPEVersion;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class CPEImport extends Command
Expand Down Expand Up @@ -36,7 +33,6 @@ public function handle()
{
$this->info('CPEImport - Start.');


$start = microtime(true);

// $file = "storage/official-cpe-dictionary_v2.3.xml";
Expand All @@ -45,9 +41,9 @@ public function handle()
$this->error('dictionary file must be specified');
return;
}
if (!file_exists ($file)) {
if (! file_exists($file)) {
$this->error('dictionary file not found');
return;
return;
}

// Delete all previous data
Expand All @@ -56,25 +52,26 @@ public function handle()
DB::table('cpe_vendors')->delete();

// count items
$items = substr_count(file_get_contents($file),"<cpe-23:cpe23-item");
$items = substr_count(file_get_contents($file), '<cpe-23:cpe23-item');
$this->info("CPEImport - {$items} CPE items to import");

// progress bar
// progress bar
$this->output->progressStart($items);

// Start parsing
$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser, $this);

xml_set_element_handler($this->xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
$this->error("could not open XML input");
xml_set_element_handler($this->xml_parser, 'startElement', 'endElement');
$fp = fopen($file, 'r');
if (! $fp) {
$this->error('could not open XML input');
return;
}

while ($data = fread($fp, 4096)) {
if (!xml_parse($this->xml_parser, $data, feof($fp))) {
$this->error("XML error: {xml_error_string(xml_get_error_code($xml_parser))} at line {xml_get_current_line_number($xml_parser)}");
if (! xml_parse($this->xml_parser, $data, feof($fp))) {
$this->error("XML error: {xml_error_string(xml_get_error_code({$xml_parser}))} at line {xml_get_current_line_number({$xml_parser})}");
return;
}
}
Expand All @@ -85,20 +82,19 @@ public function handle()

// Log time
$end = microtime(true);
$time = number_format(($end - $start), 2);
$this->info('CPEImport - elapsed time: ', $time , ' seconds');
$time = number_format($end - $start, 2);
$this->info('CPEImport - elapsed time: ', $time, ' seconds');

// Done
$this->info('CPEImport - DONE.');
}

function startElement($parser, $name, $attribs)
public function startElement($parser, $name, $attribs)
{
if ($name == "CPE-23:CPE23-ITEM") {

if ($name === 'CPE-23:CPE23-ITEM') {
$this->output->progressAdvance();

$value = explode(":",$attribs["NAME"]);
$value = explode(':', $attribs['NAME']);
// $this->info($value[2] . " " . $value[3] . ' ' . $value[4] . ' ' . $value[5]);

// check vendor exixts
Expand All @@ -110,31 +106,30 @@ function startElement($parser, $name, $attribs)
if ($vendor === null) {
$vendor = CPEVendor::create(['part' => $value[2], 'name' => $value[3]]);
}

// check product exists
$product = DB::table('cpe_products')
->where('cpe_vendor_id', '=', $vendor->id)
->where('name', '=', $value[4])
->get()->first();
// add product
if ($product === null)
if ($product === null) {
$product = CPEProduct::create(['cpe_vendor_id' => $vendor->id, 'name' => $value[4]]);
}

// check version exists
$version = DB::table('cpe_versions')
->where('cpe_product_id', '=', $product->id)
->where('name', '=', $value[5])
->get()->first();
// Add version
if ($version === null)
$version = CPEVersion::create(['cpe_product_id' => $product->id, 'name' => $value[5]]);

if ($version === null) {
CPEVersion::create(['cpe_product_id' => $product->id, 'name' => $value[5]]);
}
}

}

function endElement($parser, $name)
public function endElement($parser, $name)
{
}

}
7 changes: 0 additions & 7 deletions app/DataProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

namespace App;

use App\Document;
use App\Process;
use App\MApplication;
use App\Information;

use App\Traits\Auditable;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* App\Actor
*
*/
class DataProcessing extends Model
{
Expand Down Expand Up @@ -45,7 +39,6 @@ class DataProcessing extends Model
'controls',
];


public function processes()
{
return $this->belongsToMany(Process::class)->orderBy('identifiant');
Expand Down
3 changes: 1 addition & 2 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public function register()
{
// reportable
$this->renderable(function (Throwable $e) {
//
return response(['error' => $e->getMessage()], $e->getCode() ?: 400);
return response(['error' => $e->getMessage()], $e->getCode() ? null : 400);
});
}
}
15 changes: 7 additions & 8 deletions app/Http/Controllers/Admin/ActivityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
namespace App\Http\Controllers\Admin;

use App\Activity;
use App\Operation;
use App\Process;

use App\Http\Controllers\Controller;
use App\Http\Requests\MassDestroyActivityRequest;
use App\Http\Requests\StoreActivityRequest;
use App\Http\Requests\UpdateActivityRequest;

use App\Operation;
use App\Process;
use Gate;

use Symfony\Component\HttpFoundation\Response;

class ActivityController extends Controller
Expand All @@ -32,7 +29,7 @@ public function create()

$operations = Operation::all()->sortBy('name')->pluck('name', 'id');
$processes = Process::all()->sortBy('name')->pluck('identifiant', 'id');

return view('admin.activities.create', compact('operations', 'processes'));
}

Expand All @@ -54,8 +51,10 @@ public function edit(Activity $activity)

$activity->load('operations', 'activitiesProcesses');

return view('admin.activities.edit',
compact('operations', 'activity', 'processes'));
return view(
'admin.activities.edit',
compact('operations', 'activity', 'processes')
);
}

public function update(UpdateActivityRequest $request, Activity $activity)
Expand Down
27 changes: 13 additions & 14 deletions app/Http/Controllers/Admin/CPEController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace App\Http\Controllers\Admin;

use App\CPEProduct;
use App\CPEVendor;
use App\CPEVersion;
use App\CPEProduct;

use App\Http\Controllers\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class CPEController extends Controller
{
public function vendors(Request $request) {
public function vendors(Request $request)
{
$part = $request->query('part');

$query = CPEVendor::limit(20);
Expand All @@ -22,13 +20,14 @@ public function vendors(Request $request) {
$search = $request->query('search');
if ($search) {
$query->where('name', 'LIKE', $search . '%');
}
}

$vendors = $query->get();
return response()->json($vendors);
}
}

public function products(Request $request) {
public function products(Request $request)
{
$part = $request->query('part');
$vendor = $request->query('vendor');

Expand All @@ -41,13 +40,14 @@ public function products(Request $request) {
$search = $request->query('search');
if ($search) {
$query->where('cpe_products.name', 'LIKE', $search . '%');
}
}

$products = $query->get();
return response()->json($products);
}
}

public function versions(Request $request) {
public function versions(Request $request)
{
$part = $request->query('part');
$vendor = $request->query('vendor');
$product = $request->query('product');
Expand All @@ -66,6 +66,5 @@ public function versions(Request $request) {
}
$versions = $query->get();
return response()->json($versions);
}

}
}
}
Loading

0 comments on commit b5ae591

Please sign in to comment.