FileLoggerAware extends LoggerAwareInterface
MysqlLoggerAware extends LoggerAwareInterface
$container->share(FileLogger::class,function(){
return new NullLogger;//тут реализация file логера
});
$container->share(MysqlLogger::class,function(){
return new NullLogger;//тут реализация Mysql логера
});
$container
->inflector(FileLoggerAware::class)
->invokeMethod('setLogger', [FileLogger::class])
;
$container
->inflector(MysqlLoggerAware::class)
->invokeMethod('setLogger', [MysqlLogger::class])
;
class MyClass implements FileLoggerAware
{
use LoggerAwareTrait;
}
$searchCriteria = new CustomerSearchCriteria;
$searchCriteria->setByName("москва");
$customers = $customerRepository->findByCriteria($searchCriteria);//->CustomerCollection
Как вы могли догадаться, бывает так что клиента в платеже нет. Был удален, например.
public function printCustomerFirstname(Payment $payment)
{
if ($payment->getCustomer()) {
echo $payment->getCustomer()->getFirstname();
}
}
public function getCustomerFirstname(): string
{
if(!$this->getCustomer()){
throw new \Exceptions\CustomerNotFound;
}
return $this->getCustomer()->getFirstname();
}
try{
$customerName = $payment->getCustomerFirstname();
}
catch( \Exceptions\CustomerNotFound $ex){
$customerName = null;
}
А можно избавиться от строки $obj = new MyClass(); и создавать экземпляр автоматически?
function __constuct(MyClass $obj){
$this->obj = $obj;
}
namespace Repository;
class User {
protected $db;
public function __construct(Adapter $db) {
$this->db = $db;
}
public function getUserById($id) {
$sql = "SELECT * FROM user WHERE id = :id";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch();
}
public function getImageById($id) {
$sql = "SELECT IF(image IS NULL or image = '','no_image.png',image) as image FROM `user` WHERE id = :id";
$result = $this->db->prepare($sql);
$result->bindParam(':id', $id, PDO::PARAM_INT);
$result->execute();
return $result->fetchColumn();
}
public function updateImageById($imageName, $id) {
$sql = "UPDATE user SET image = '$imageName' WHERE id = $id";
return $this->db->query($sql);
}
}
namespace Service;
class Image {
protected $userRepo;
function __construct(Repository\User $userRepo) {
$this->userRepo = $userRepo;
}
public function UploadImage(array $image) {
$usersImage = $image['userfile']['tmp_name'];
$imageName = $image['userfile']['name'];
if (is_uploaded_file($image['userfile']['tmp_name'])) {
move_uploaded_file($usersImage, $_SERVER['DOCUMENT_ROOT'] . "/template/images/users/" . $image['userfile']['name']);
}
return $imageName;
}
public function updateImage(array $image, $id) {
if($imageName = $this->uploadImage($image, $id)) {
$this->userRepo->updateImageById($imageName, $id);
return true;
}
}
}
class UserController extends BaseController {
protected $imageService;
protected $userRepo;
function __construct(Service\Image $imageService, Repository\User $userRepo) {
$this->imageService = $imageService;
$this->userRepo = $userRepo;
}
public function actionProfile($id) {
if (isset($_POST['submit_photo'])) {
$this->imageService->updateImage($_FILES, $id);
}
return $this->render('user/profile.php', [
'user' => $this->userRepo->getUserById($id),
'image' => $this->userRepo->getImageById($id)
]);
}
}
Controller{
protected $cabinetRepo;
protected $zapisRepo;
function __constructor(CabinetRepository $cabinetRepo, ZapisRepository $zapisRepo ){
$this->zapisRepo = $zapisRepo;
$this->cabinetRepo = $cabinetRepo;
}
public function action(){
$zapisCollection = $this->zapisRepo->findOnCurrentDay();
}