Skip to content
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

fix code #34

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions php/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
3 changes: 3 additions & 0 deletions php/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
.idea
src/config.php
56 changes: 0 additions & 56 deletions php/1.php

This file was deleted.

90 changes: 0 additions & 90 deletions php/2.php

This file was deleted.

15 changes: 15 additions & 0 deletions php/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "denis/testtask",
"autoload": {
"psr-4": {
"": "src/"
}
},
"authors": [
{
"name": "Denis Oleshkevich",
"email": "denis@oleshkevich.ru"
}
],
"require": {}
}
45 changes: 45 additions & 0 deletions php/src/Db/PdoConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Db;

use PDO;

class PdoConnection
{

private static ?PDO $pdoInstance = null;

private const CONFIG_FILE = "config.php";

private static function loadConfig($configFile)
{
if (!is_readable($configFile)) {
throw new \Exception(sprintf('Config file %s is not found', $configFile));
}
return include_once($configFile);
}


public static function init($configFile)
{
$config = self::loadConfig($configFile);

self::$pdoInstance = new PDO($config['dsn'], $config['user'], $config['password']);

self::$pdoInstance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}

/**
* Реализация singleton
* @return PDO
*/
//ну это не синглтон, а хранение экземляра PDO. синглтон хранит экзепляпляр на тот же класс - на Gateway/User в нашем случае
public static function getPdoInstance(): PDO
{
if (is_null(self::$pdoInstance)) {
throw new \Exception("Use init() at first call");
}

return self::$pdoInstance;
}
}
62 changes: 62 additions & 0 deletions php/src/Manager/UserManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Manager;

use Model\UserModel;

final class UserManager
{

const MAX_NAMES_FOR_FILTERING = 100;

/**
* Возвращает пользователей старше заданного возраста.
* @param int $ageFrom
* @return array
*/
function filterUsersFromAge(int $ageFrom): array
{
return UserModel::filterUsersFromAge($ageFrom);
}

/**
* Возвращает пользователей по списку имен.
* @return array
*/
public static function filterByNames(): array
{
if (!array_key_exists('names', $_GET)) {
return [];
}

$names = $_GET['names'];

$names = array_slice($names, 0, self::MAX_NAMES_FOR_FILTERING);

return UserModel::filterByNames($names);
}

/**
* Добавляет пользователей в базу данных.
* @param $users
* @return array
*/
public function addUsers($users): array
{
$ids = [];

try {
UserModel::getPdoInstance()->beginTransaction();
foreach ($users as $user) {
$ids[] = UserModel::add($user['name'], $user['lastName'], $user['age']);
}
UserModel::getPdoInstance()->commit();
} catch (\Exception $e) {
UserModel::getPdoInstance()->rollBack();

throw $e;
}

return $ids;
}
}
14 changes: 14 additions & 0 deletions php/src/Model/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Model;

use Db\PdoConnection;

class Model
{

public static function getPdoInstance(): \PDO
{
return PdoConnection::getPdoInstance();
}
}
Loading