Skip to content

Commit

Permalink
add RepositoryInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
albertborsos committed Jun 27, 2019
1 parent b69022b commit 312f277
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/interfaces/RepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace albertborsos\ddd\interfaces;

use yii\db\ActiveRecordInterface;

interface RepositoryInterface
{
public static function find();

public static function findOne($condition);

public static function findAll($condition);

public static function updateAll($attributes, $condition = null);

public static function deleteAll($condition = null);

public function save(ActiveRecordInterface $model, $runValidation = true, $attributeNames = null);

public function insert(ActiveRecordInterface $model, $runValidation = true, $attributeNames = null);

public function update(ActiveRecordInterface $model, $runValidation = true, $attributeNames = null);

public function delete(ActiveRecordInterface $model);
}
28 changes: 26 additions & 2 deletions src/models/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use albertborsos\ddd\interfaces\BusinessObject;
use albertborsos\ddd\interfaces\FormObject;
use albertborsos\ddd\interfaces\RepositoryInterface;
use yii\base\Component;
use yii\helpers\ArrayHelper;
use yii\web\Link;
Expand Down Expand Up @@ -31,14 +32,22 @@ abstract class AbstractService extends Component
*/
private $_model;

public function __construct(FormObject $form = null, BusinessObject $model = null)
/**
* @var RepositoryInterface
*/
private $_repository;

public function __construct(FormObject $form = null, BusinessObject $model = null, RepositoryInterface $repository = null)
{
if ($form) {
$this->setForm($form);
}
if ($model) {
$this->setModel($model);
}
if ($repository) {
$this->setRepository($repository);
}
parent::__construct([]);
}

Expand All @@ -55,7 +64,6 @@ protected function getForm()
return $this->_form;
}


/**
* @return BusinessObject|ActiveRecord
*/
Expand All @@ -64,6 +72,14 @@ protected function getModel()
return $this->_model;
}

/**
* @return RepositoryInterface
*/
protected function getRepository()
{
return $this->_repository;
}

/**
* @param $id
*/
Expand Down Expand Up @@ -95,4 +111,12 @@ private function setModel(BusinessObject $model)
{
$this->_model = $model;
}

/**
* @param RepositoryInterface $repository
*/
private function setRepository(RepositoryInterface $repository)
{
$this->_repository = $repository;
}
}

0 comments on commit 312f277

Please sign in to comment.