Skip to content

Apply fixes from StyleCI #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Code/Base.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace CodeDredd\Soap\Code;

use CodeDredd\Soap\Types\Service;
Expand All @@ -7,12 +8,10 @@
use Laminas\Code\Generator\FileGenerator;

/**
* Class Base
* @package CodeDredd\Soap\Code
* Class Base.
*/
class Base
{

/**
* @var \Illuminate\Support\Collection
*/
Expand Down Expand Up @@ -67,18 +66,20 @@ public function __construct(Service $engine, $configName)
/**
* @return string
*/
public function getCode() {
public function getCode()
{
return $this->codeClass->generate();
}

/**
* @param $filePath
*/
public function saveFile($filePath) {
$file = config('soap.code.path', app_path('Soap')) . $filePath;
if (!file_exists($file)) {
public function saveFile($filePath)
{
$file = config('soap.code.path', app_path('Soap')).$filePath;
if (! file_exists($file)) {
$fileCode = FileGenerator::fromArray([
'classes' => [$this->codeClass]
'classes' => [$this->codeClass],
]);
mkdir(dirname($file), 0777, true);
file_put_contents($file, $fileCode->generate());
Expand Down
57 changes: 30 additions & 27 deletions src/Code/Client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace CodeDredd\Soap\Code;

use CodeDredd\Soap\Facades\Soap;
Expand All @@ -15,8 +16,7 @@
use Wsdl2PhpGenerator\Operation;

/**
* Class Client
* @package CodeDredd\Soap\Code
* Class Client.
*/
class Client extends Base
{
Expand Down Expand Up @@ -48,45 +48,46 @@ public function createNewClient()
$className = ucfirst(Str::camel($this->configName).'Client');
$methodTags = $this->actions->map(function (Operation $action) {
$params = $action->getParams() > 0 ? '($body = [])' : '()';

return new GenericTag(
'method',
'CodeDredd\\Soap\\Client\\Response ' . $action->getName() . $params
'CodeDredd\\Soap\\Client\\Response '.$action->getName().$params
);
})->values()->toArray();
$docBlock = DocBlockGenerator::fromArray([
'shortDescription' => $this->clientClassName.' Client',
'tags' => $methodTags
'tags' => $methodTags,
]);
$constructorDocBlock = DocBlockGenerator::fromArray([
'shortDescription' => $className.' constructor'
'shortDescription' => $className.' constructor',
]);
$callDocBlock = DocBlockGenerator::fromArray([
'shortDescription' => 'Execute soap call',
'tags' => [
new ParamTag('method', 'string'),
new ParamTag('parameters', 'mixed'),
new ReturnTag('\CodeDredd\Soap\Client\Response|mixed')
]
new ReturnTag('\CodeDredd\Soap\Client\Response|mixed'),
],
]);
$callMethodParameters = [
'method',
'parameters'
'parameters',
];
$callMethodBody = 'if (static::hasMacro($method)) {'."\n "
.'return $this->macroCall($method, $parameters);'."\n"
.'}'."\n\n"
. '$validationClass = \'CodeDredd\\\\Soap\\\\Soap\\\\Validations\\\\LaravelSoap\\\\\''."\n "
. '. ucfirst(Str::camel($method))'."\n "
. '. \'Validation\';'."\n"
.'$validationClass = \'CodeDredd\\\\Soap\\\\Soap\\\\Validations\\\\LaravelSoap\\\\\''."\n "
.'. ucfirst(Str::camel($method))'."\n "
.'. \'Validation\';'."\n"
.'if (class_exists($validationClass)) {'."\n "
. '$parameters = app()->call([$validationClass, \'validator\'], [\'parameters\' => $parameters]);'."\n"
. '}'."\n\n"
.'$parameters = app()->call([$validationClass, \'validator\'], [\'parameters\' => $parameters]);'."\n"
.'}'."\n\n"
.'return $this->client->call($method, $parameters);';

$this->codeClass->setName($className)
->setDocBlock($docBlock)
->setNamespaceName($this->codeNamespace . '\\Clients')
->setImplementedInterfaces([$this->codeNamespace . '\\Contracts\\' . $this->clientClassName . 'Contract'])
->setNamespaceName($this->codeNamespace.'\\Clients')
->setImplementedInterfaces([$this->codeNamespace.'\\Contracts\\'.$this->clientClassName.'Contract'])
->addProperty('client', null, PropertyGenerator::FLAG_PROTECTED)
->addUse(Soap::class)
->addUse(Macroable::class)
Expand All @@ -106,36 +107,38 @@ public function createNewClient()
* @param Operation $action
* @return MethodGenerator
*/
public static function createNewAction(Operation $action) {
public static function createNewAction(Operation $action)
{
$validationClass = ucfirst(Str::camel($action->getName()).'Validation');
$actionBody = 'return $this->client->call(\'' . $action->getName()
. '\', ' . $validationClass . '::validator($body));';
$actionBody = 'return $this->client->call(\''.$action->getName()
.'\', '.$validationClass.'::validator($body));';

return new MethodGenerator(
$action->getName(),
empty($action->getParams()) ? [] : [
[
'name' => 'body',
'defaultValue' => []
]
'defaultValue' => [],
],
],
MethodGenerator::FLAG_PUBLIC,
$actionBody,
DocBlockGenerator::fromArray([
'shortDescription' => 'Call action ' . $action->getName(),
'shortDescription' => 'Call action '.$action->getName(),
'tags' => [
new ParamTag('body', 'array'),
new ReturnTag('\CodeDredd\Soap\Client\Response')
]
new ReturnTag('\CodeDredd\Soap\Client\Response'),
],
])
);
}

/**
* Save generated code as file
* Save generated code as file.
*/
public function save() {
public function save()
{
$this->clientContract->save();
$this->saveFile('/Clients/' . $this->clientClassName .'Client.php');
$this->saveFile('/Clients/'.$this->clientClassName.'Client.php');
}

}
20 changes: 12 additions & 8 deletions src/Code/ClientContract.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace CodeDredd\Soap\Code;

use CodeDredd\Soap\Types\Service;
Expand All @@ -9,8 +10,7 @@
use Wsdl2PhpGenerator\Operation;

/**
* Class ClientContract
* @package CodeDredd\Soap\Code
* Class ClientContract.
*/
class ClientContract extends Base
{
Expand All @@ -29,27 +29,31 @@ public function __construct(Service $engine, $configName)
/**
* @return InterfaceGenerator
*/
public function createNewClientContract() {
public function createNewClientContract()
{
$className = ucfirst(Str::camel($this->configName).'Contract');
$methodTags = $this->actions->map(function (Operation $action) {
$params = $action->getParams() > 0 ? '($body = []) ' : '() ';

return new GenericTag(
'method',
'CodeDredd\\Soap\\Client\\Response ' . $action->getName() . $params . $action->getDescription()
'CodeDredd\\Soap\\Client\\Response '.$action->getName().$params.$action->getDescription()
);
})->values()->toArray();
$docBlock = DocBlockGenerator::fromArray([
'shortDescription' => $this->clientClassName.' Contract',
'tags' => $methodTags
'tags' => $methodTags,
]);

return $this->codeClass->setName($className)
->setDocBlock($docBlock);
}

/**
* Save generated code as file
* Save generated code as file.
*/
public function save() {
$this->saveFile('/Contracts/' . $this->clientClassName .'Contract.php');
public function save()
{
$this->saveFile('/Contracts/'.$this->clientClassName.'Contract.php');
}
}
51 changes: 28 additions & 23 deletions src/Code/Validation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace CodeDredd\Soap\Code;

use CodeDredd\Soap\Types\Service;
Expand All @@ -14,8 +15,7 @@
use Wsdl2PhpGenerator\Variable;

/**
* Class Validation
* @package CodeDredd\Soap\Code
* Class Validation.
*/
class Validation extends Base
{
Expand Down Expand Up @@ -49,7 +49,7 @@ public function __construct(Service $engine, $configName, $dryRun = false)
public function createNewValidation(Operation $action)
{
$this->actionName = $action->getName();
$className = ucfirst(Str::camel($action->getName()) . 'Validation');
$className = ucfirst(Str::camel($action->getName()).'Validation');
$validationArray = [];
if (count($action->getParams()) > 0) {
foreach ($action->getParams() as $key => $param) {
Expand All @@ -62,18 +62,17 @@ public function createNewValidation(Operation $action)
$validationArray[] = $validationForParam;
}
$validationArray = Arr::dot($validationArray);

}
$validatorFlags = [MethodGenerator::FLAG_PUBLIC, MethodGenerator::FLAG_STATIC];
$validatorBody = 'return Validator::make($parameters, [' . "\n"
. $this->arrayToStringCode($validationArray)
. ']);';
$validatorBody = 'return Validator::make($parameters, ['."\n"
.$this->arrayToStringCode($validationArray)
.']);';
$docBlock = DocBlockGenerator::fromArray([
'shortDescription' => $this->actionName.' Validation',
'longDescription' => $action->getDescription()
'longDescription' => $action->getDescription(),
]);
$this->codeClass->setName($className)
->setNamespaceName($this->codeNamespace . '\\Validations\\' . $this->clientClassName)
->setNamespaceName($this->codeNamespace.'\\Validations\\'.$this->clientClassName)
->setDocBlock($docBlock)
->addUse(Validator::class)
->addMethod('validator', ['parameters'], $validatorFlags, $validatorBody);
Expand All @@ -84,10 +83,11 @@ public function createNewValidation(Operation $action)
/**
* @param array $actionNames
*/
public function generateValidationFiles(array $actionNames = []) {
public function generateValidationFiles(array $actionNames = [])
{
foreach ($actionNames as $actionName) {
$action = $this->actions->get($actionName);
if (!empty($action)) {
if (! empty($action)) {
$this->codeClass = $this->createNewValidation($action);
$this->dryRun ? print_r($this->getCode()) : $this->save();
}
Expand All @@ -98,13 +98,15 @@ public function generateValidationFiles(array $actionNames = []) {
* @param array $array
* @return string
*/
protected function arrayToStringCode(array $array) {
protected function arrayToStringCode(array $array)
{
$stringCode = '';
foreach ($array as $key => $value) {
if (!empty($value)) {
$stringCode .= " '" . $key . '\' => \'' . $value . "',\n";
if (! empty($value)) {
$stringCode .= " '".$key.'\' => \''.$value."',\n";
}
}

return $stringCode;
}

Expand All @@ -121,10 +123,10 @@ public function generateValidationArrayByAction(array $properties, $validationAr
if ($propertyType instanceof ComplexType) {
$validationArray['*'] = 'filled';
$validationArray[$property->getName()] = $this->generateValidationArrayByAction($propertyType->getMembers());
} elseif($propertyType instanceof Enum) {
$validationArray[$property->getName()] = 'in:' . $propertyType->getValidValues();
} elseif ($propertyType instanceof Enum) {
$validationArray[$property->getName()] = 'in:'.$propertyType->getValidValues();
} else {
$validationArray[$property->getName()] = $this->mapToValidType($property->getType()) . ($property->getNullable() ? '|nullable' : '');
$validationArray[$property->getName()] = $this->mapToValidType($property->getType()).($property->getNullable() ? '|nullable' : '');
}
}

Expand All @@ -135,7 +137,8 @@ public function generateValidationArrayByAction(array $properties, $validationAr
* @param $type
* @return string
*/
public function mapToValidType($type) {
public function mapToValidType($type)
{
switch ($type) {
case 'datetime': return 'date_format:Y-m-d H:i:s';
case 'date': return 'date';
Expand All @@ -152,17 +155,19 @@ public function mapToValidType($type) {
/**
* @return string
*/
public function getCode() {
public function getCode()
{
return $this->codeClass->generate();
}

/**
* Save generated code as file
* Save generated code as file.
*/
public function save() {
public function save()
{
$this->saveFile(
'/Validations/' . $this->clientClassName . '/'
. ucfirst(Str::camel($this->actionName).'Validation.php')
'/Validations/'.$this->clientClassName.'/'
.ucfirst(Str::camel($this->actionName).'Validation.php')
);
}
}
Loading