Created
November 26, 2019 01:49
-
-
Save dersonsena/07cab7554cd90b65d14f66239fe2b882 to your computer and use it in GitHub Desktop.
Repository Approach in Yii2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://gitlab.com/getto/go-sale/blob/develop/services/auth/src/Http/Location/CitiesAction.php | |
namespace App\Http\Location; | |
use Yii; | |
use App\Domain\Location\CityRepository; | |
use yii\base\Action; | |
class CitiesAction extends Action | |
{ | |
/** | |
* @var CityRepository | |
*/ | |
protected $repository; | |
public function init() | |
{ | |
$this->repository = Yii::$container->get(CityRepository::class); | |
parent::init(); | |
} | |
public function run() | |
{ | |
$stateUuid = Yii::$app->request->get('state_uuid'); | |
Yii::$app->response->setStatusCode(200); | |
return $this->repository->getCitiesByState($stateUuid); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://gitlab.com/getto/go-sale/blob/develop/services/auth/src/Domain/Location/City.php | |
namespace App\Domain\Location; | |
use GoSale\YiiCore\ActiveRecord\ActiveRecordAbstract; | |
/** | |
* This is the model class for table "{{%cities}}". | |
* | |
* @property string $id (DC2Type:uuid_binary_ordered_time) | |
* @property string $uuid (DC2Type:uuid) | |
* @property string $state_id (DC2Type:uuid_binary_ordered_time) | |
* @property string $name | |
* @property int $status | |
* | |
* @property State $state | |
*/ | |
class City extends ActiveRecordAbstract | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function tableName() | |
{ | |
return '{{%cities}}'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['id', 'uuid', 'state_id', 'name'], 'required'], | |
[['status'], 'integer'], | |
[['id', 'state_id'], 'string', 'max' => 16], | |
[['uuid'], 'string', 'max' => 36], | |
[['name'], 'string', 'max' => 60], | |
[['uuid'], 'unique'], | |
[['id'], 'unique'], | |
[ | |
'state_id', | |
'exist', | |
'skipOnError' => true, | |
'targetClass' => State::class, | |
'targetAttribute' => ['state_id' => 'id'] | |
], | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function attributeLabels() | |
{ | |
return $this->buildAttributeLabels([ | |
'state_id' => 'State ID', | |
'name' => 'Nome', | |
]); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getState() | |
{ | |
return $this->hasOne(State::class, ['id' => 'state_id']); | |
} | |
public function fields() | |
{ | |
$fields = parent::fields(); | |
$fields['state'] = 'state'; | |
unset($fields['status']); | |
return $fields; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://gitlab.com/getto/go-sale/blob/develop/services/auth/src/Domain/Location/CityRepository.php | |
declare(strict_types=1); | |
namespace App\Domain\Location; | |
use Yii; | |
use GoSale\YiiCore\Repository\RepositoryAbstract; | |
use yii\caching\CacheInterface; | |
use yii\helpers\Json; | |
class CityRepository extends RepositoryAbstract | |
{ | |
/** | |
* @var string | |
*/ | |
protected $modelClass = City::class; | |
/** | |
* @param string $uuid | |
* @return array | |
*/ | |
public function getCitiesByState(string $uuid): array | |
{ | |
/** @var CacheInterface $cache */ | |
$cache = Yii::$app->cache; | |
$cities = $cache->get('cities'); | |
if ($cities === false) { | |
/** @var City[] $cities */ | |
$cities = $this->model::find() | |
->orderBy('name ASC') | |
->all(); | |
$cities = Json::encode($cities); | |
$cache->set("cities", $cities); | |
} | |
$cities = Json::decode($cache->get('cities')); | |
return array_filter($cities, function ($city) use ($uuid) { | |
return $city['state']['uuid'] === $uuid; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace GoSale\YiiCore\Repository; | |
use GoSale\YiiCore\ActiveRecord\ActiveRecordAbstract; | |
use yii\db\Exception; | |
// https://gitlab.com/getto/go-sale/blob/develop/packages/php/Stacks/Yii/src/Repository/RepositoryAbstract.php | |
abstract class RepositoryAbstract | |
{ | |
/** | |
* @var string | |
*/ | |
protected $modelClass; | |
/** | |
* @var ActiveRecordAbstract | |
*/ | |
protected $model; | |
public function __construct() | |
{ | |
if (empty($this->modelClass)) { | |
throw new Exception('The "$model" attribute is required.'); | |
} | |
$this->model = new $this->modelClass; | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function find() | |
{ | |
return $this->model::find(); | |
} | |
/** | |
* @param $condition | |
* @return ActiveRecordAbstract|null | |
*/ | |
public function findOne($condition) | |
{ | |
return $this->model::findOne($condition); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment