Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Sep 13, 2016
0 parents commit 79e8de2
Show file tree
Hide file tree
Showing 14 changed files with 434 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
phpunit/
vendor/
composer.lock

.idea/
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: php

php:
- 7.0

before_script:
- composer --prefer-source --dev install

script: phpunit --coverage-text --verbose
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 Dominik Zogg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# saxulum-message-queue

[![Build Status](https://api.travis-ci.org/saxulum/saxulum-message-queue.png?branch=master)](https://travis-ci.org/saxulum/saxulum-message-queue)
[![Total Downloads](https://poser.pugx.org/saxulum/saxulum-message-queue/downloads.png)](https://packagist.org/packages/saxulum/saxulum-message-queue)
[![Latest Stable Version](https://poser.pugx.org/saxulum/saxulum-message-queue/v/stable.png)](https://packagist.org/packages/saxulum/saxulum-message-queue)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/saxulum/saxulum-message-queue/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/saxulum/saxulum-message-queue/?branch=master)

## Description

A simple to use messaging queue abstraction.

## Requirements

* php: ~7.0

## Installation

Through [Composer](http://getcomposer.org) as [saxulum/saxulum-message-queue][1].

## Usage

[1]: https://packagist.org/packages/saxulum/saxulum-message-queue

## Copyright

Dominik Zogg 2016
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "saxulum/saxulum-message-queue",
"description": "Saxulum Message Queue",
"keywords": ["saxulum", "message-queue", "system-v"],
"license": "MIT",
"authors": [
{
"name": "Dominik Zogg",
"email": "[email protected]"
}
],
"require": {
"php": "~7.0"
},
"require-dev": {
"phpunit/phpunit": "~5.0"
},
"autoload": {
"psr-4": { "Saxulum\\MessageQueue\\": "src/" }
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
26 changes: 26 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Saxulum Message Queue">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="phpunit/coverage" />
</logging>
</phpunit>
17 changes: 17 additions & 0 deletions src/MessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Saxulum\MessageQueue;

interface MessageInterface
{
/**
* @param string $json
* @return MessageInterface
*/
public static function fromJson(string $json): MessageInterface;

/**
* @return string
*/
public function toJson(): string;
}
11 changes: 11 additions & 0 deletions src/MessageReceiveInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Saxulum\MessageQueue;

interface MessageReceiveInterface
{
/**
* @return MessageInterface|null
*/
public function receive();
}
12 changes: 12 additions & 0 deletions src/MessageSendInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Saxulum\MessageQueue;

interface MessageSendInterface
{
/**
* @param MessageInterface $message
* @return MessageSendInterface
*/
public function send(MessageInterface $message): MessageSendInterface;
}
64 changes: 64 additions & 0 deletions src/SystemV/SystemVReceive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Saxulum\MessageQueue\SystemV;

use Saxulum\MessageQueue\MessageInterface;
use Saxulum\MessageQueue\MessageReceiveInterface;

final class SystemVReceive implements MessageReceiveInterface
{
/**
* @var string
*/
private $messageClass;

/**
* @var resource
*/
private $queue;

/**
* @var int
*/
private $type;

/**
* @param string $messageClass
* @param int $key
* @param int $type
* @throws \Exception
*/
public function __construct(string $messageClass, int $key, int $type = 1)
{
$this->messageClass = $messageClass;
$this->queue = msg_get_queue($key);
$this->type = $type;
}

/**
* @return null|MessageInterface
* @throws \Exception
*/
public function receive()
{
$type = null;
$json = null;
$error = null;

$status = msg_receive($this->queue, $this->type, $type, 1048576, $json, false, MSG_IPC_NOWAIT, $error);

if (false === $status) {
// we do not wait for a message (prevent lock)
if (MSG_ENOMSG === $error) {
return null;
}

throw new \Exception(sprintf('Can\'t receive message, error code %d', $error));
}

/** @var MessageInterface $messageClass */
$messageClass = $this->messageClass;

return $messageClass::fromJson($json);
}
}
45 changes: 45 additions & 0 deletions src/SystemV/SystemVSend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Saxulum\MessageQueue\SystemV;

use Saxulum\MessageQueue\MessageInterface;
use Saxulum\MessageQueue\MessageSendInterface;

final class SystemVSend implements MessageSendInterface
{
/**
* @var resource
*/
private $queue;

/**
* @var int
*/
private $type;

/**
* @param int $key
* @param int $type
* @throws \Exception
*/
public function __construct(int $key, int $type = 1)
{
$this->queue = msg_get_queue($key);
$this->type = $type;
}

/**
* @param MessageInterface $message
* @return MessageSendInterface
* @throws \Exception
*/
public function send(MessageInterface $message): MessageSendInterface
{
$json = $message->toJson();
if (false === msg_send($this->queue, $this->type, $json, false)) {
throw new \Exception(sprintf('Cant send message : %s', $json));
}

return $this;
}
}
99 changes: 99 additions & 0 deletions tests/Resources/SampleMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Saxulum\Tests\MessageQueue\Resources;

use Saxulum\MessageQueue\MessageInterface;

class SampleMessage implements MessageInterface
{
/**
* @var string
*/
private $context;

/**
* @var int
*/
private $total;

/**
* @var int
*/
private $success;

/**
* @var int
*/
private $failed;

/**
* @param string $json
*
* @return MessageInterface
*/
public static function fromJson(string $json): MessageInterface
{
$rawMessage = json_decode($json);

return new self($rawMessage->context, $rawMessage->total, $rawMessage->success, $rawMessage->failed);
}

/**
* @return string
*/
public function toJson(): string
{
return json_encode([
'context' => $this->context,
'total' => $this->total,
'success' => $this->success,
'failed' => $this->failed,
]);
}

/**
* @param string $context
* @param int $total
* @param int $success
* @param int $failed
*/
public function __construct(string $context, int $total, int $success, int $failed)
{
$this->context = $context;
$this->total = $total;
$this->success = $success;
$this->failed = $failed;
}

/**
* @return string
*/
public function getContext(): string
{
return $this->context;
}

/**
* @return int
*/
public function getTotal(): int
{
return $this->total;
}

/**
* @return int
*/
public function getSuccess(): int
{
return $this->success;
}

/**
* @return int
*/
public function getFailed(): int
{
return $this->failed;
}
}
Loading

0 comments on commit 79e8de2

Please sign in to comment.