Exemplo n.º 1
0
 public function validateStudent(Student $student)
 {
     $errors = [];
     $errors['firstName'] = $this->validateName($student->getFirstName());
     $errors['lastName'] = $this->validateName($student->getLastName());
     $errors['gender'] = $this->validateGender($student->getGender());
     $errors['group'] = $this->validateGroup($student->getGroup());
     $errors['email'] = $this->confirmEmail($student->getEmail(), $student->getToken());
     $errors['birthYear'] = $this->validateBirthYear($student->getBirthYear());
     $errors['status'] = $this->validateStatus($student->getStatus());
     $errors['rating'] = $this->validateRating($student->getRating());
     return array_filter($errors, [$this, 'filterErrors']);
 }
 /**
  * Updates given Student entity in the database.
  *
  * @param Student $student Student entity to update.
  */
 public function updateStudent(Student $student)
 {
     $query = $this->pdo->prepare("UPDATE students SET name = :name_bind, surname = :surname_bind, " . "gender = :gender_bind, sgroup = :sgroup_bind, email = :email_bind, byear = :byear_bind, status = :status_bind, " . "rating = :rating_bind WHERE token = :token_bind");
     $query->bindValue(':token_bind', $student->getToken(), \PDO::PARAM_STR);
     $query->bindValue(':name_bind', $student->getFirstName(), \PDO::PARAM_STR);
     $query->bindValue(':surname_bind', $student->getLastName(), \PDO::PARAM_STR);
     $query->bindValue(':gender_bind', $student->getGender(), \PDO::PARAM_STR);
     $query->bindValue(':sgroup_bind', $student->getGroup(), \PDO::PARAM_STR);
     $query->bindValue(':email_bind', $student->getEmail(), \PDO::PARAM_STR);
     $query->bindValue(':byear_bind', $student->getBirthYear(), \PDO::PARAM_INT);
     $query->bindValue(':status_bind', $student->getStatus(), \PDO::PARAM_STR);
     $query->bindValue(':rating_bind', $student->getRating(), \PDO::PARAM_INT);
     $query->execute();
 }
Exemplo n.º 3
0
    $fileRaw = file($filename);
    if ($fileRaw == false) {
        throw new \Exception("Can't read file {$filename}." . PHP_EOL);
    }
    $fileTrimmed = array_map("trim", $fileRaw);
    $fileFiltered = array_filter($fileTrimmed, "checkStringCallback");
    return $fileFiltered;
}
function selectRandomElement($array)
{
    return $array[mt_rand(0, count($array) - 1)];
}
$options = getopt("c:h");
if (!isset($options["c"]) || $options["c"] <= 0 || isset($options["h"])) {
    die(outputHelpMessage($argv));
}
$names = readNamesFile("./resource/names.txt");
$surnames = readNamesFile("./resource/surnames.txt");
for ($i = 0; $i < $options["c"]; $i++) {
    $student = new Student();
    $student->setFirstName(selectRandomElement($names));
    $student->setLastName(selectRandomElement($surnames));
    $student->setGender(Student::GENDER_MALE);
    $student->setGroup(getRandomString(3, 5));
    $student->setEmail(sprintf("*****@*****.**", getRandomString(5, 13)));
    $student->setBirthYear(sprintf("19%d%d", mt_rand(0, 9), mt_rand(0, 9)));
    $student->setStatus(Student::STATUS_RESIDENT);
    $student->setRating(mt_rand(0, StudentValidator::STUDENT_MAX_RATING));
    $container["studentGateway"]->addStudent($student);
}
print "Database was successfully filled for {$options['c']} entries." . PHP_EOL;
 /**
  * Authorizes given Student entity.
  *
  * This method authorizes user using cookies. In details, it adds an
  * authorization cookie to the given response instance. Authorization
  * cookie contains an authorization token, which is unique for every student
  * entity.
  *
  * This method throws an InvalidArgumentException if given student entity does
  * not have an authorization token. You can use createAuthToken() method to generate
  * an auth token for a student entity.
  *
  * @param Student $student Student to authorize
  * @param ResponseInterface $response Response instance for authorization cookie.
  *
  * @throws \InvalidArgumentException If given Student instance does not have an
  * authorization token.
  *
  * @return \Psr\Http\Message\ResponseInterface Response instance with authorization cookie.
  */
 public function authorizeUser(Student $student, ResponseInterface $response)
 {
     if (empty($student->getToken())) {
         throw new \InvalidArgumentException('Student must have an authorization' . ' token in order to complete authorization.');
     }
     $dateTime = new \DateTime("now");
     $dateTime->add(new \DateInterval("P90D"));
     return FigResponseCookies::set($response, SetCookie::create('authorization')->withValue($student->getToken())->withExpires($dateTime->format(\DateTime::COOKIE))->withPath('/'));
 }
Exemplo n.º 5
0
use Students\Exception\ApplicationException;
$app = new Application();
// Map routes
$app->route('/', 'GET', function (Request $request, Response $response) use($container) {
    $searchQuery = $request->getQueryParam('search', '');
    $pager = new Pagination($container['studentGateway']->getTotalStudents($searchQuery), 15);
    $page = $request->getQueryParam('page', 1);
    $page = $pager->validatePageNumber($page);
    $students = $container['studentGateway']->searchStudents($searchQuery, $pager->getOffset($page), $pager->getLimit(), $request->filterQueryParam('key', ['id', 'name', 'surname', 'sgroup', 'rating'], 'rating'), $request->filterQueryParam('type', ['asc', 'desc'], 'desc'));
    return $container['view']->renderTemplate('index.phtml', $response, ['linker' => new LinkGenerator($request), 'students' => $students, 'pager' => $pager, 'page' => $page, 'authorized' => $container['studentAuthorization']->isAuthorized($request), 'student' => $container['studentGateway']->selectStudent($container['studentAuthorization']->getAuthToken($request)), 'notification' => $request->filterQueryParam('notification', ['added', 'edited']), 'searchQuery' => $searchQuery]);
});
$app->route('/form', ['GET', 'POST'], function (Request $request, Response $response) use($container) {
    $auth = $container['studentAuthorization'];
    $gateway = $container['studentGateway'];
    $csrfProtection = $container['csrfProtection'];
    $response = $csrfProtection->setResposneCookie($response);
    $student = !empty($auth->getAuthToken($request)) ? $gateway->selectStudent($auth->getAuthToken($request)) : new Student();
    if ($request->getMethod() === 'POST') {
        $csrfProtection->validateCsrfToken($request);
        $student = Student::fromPostRequest($request);
        $student->setToken($auth->getToken($request));
        $errors = $container['studentValidator']->validateStudent($student);
        if (empty($errors)) {
            $auth->isAuthorized($request) ? $gateway->updateStudent($student) : $gateway->addStudent($student);
            $response = $auth->authorizeUser($student, $response);
            return $response->withHeader('Location', '/' . "?notification=" . ($auth->isAuthorized($request) ? 'edited' : 'added'));
        }
    }
    return $container['view']->renderTemplate('form.phtml', $response, ['student' => $student, 'errors' => isset($errors) ? $errors : [], 'csrfToken' => $csrfProtection->getCsrfToken(), 'authorized' => $container['studentAuthorization']->isAuthorized($request)]);
});
$app->start();