Merge pull request #627 from MatthewVita/message_broken_link_fix
[openemr.git] / services / product_registration_service.php
blobafc13afb9e2e0754ede2c88e06a16743012a8f8a
1 <?php
2 /**
3 * ProductRegistrationService
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
16 * @package OpenEMR
17 * @author Matthew Vita <matthewvita48@gmail.com>
18 * @author Victor Kofia <victor.kofia@gmail.com>
19 * @link http://www.open-emr.org
22 namespace services;
24 require_once($GLOBALS['fileroot'] . "/interface/main/exceptions/invalid_email_exception.php");
25 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/generic_product_registration_exception.php");
26 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/duplicate_registration_exception.php");
28 class ProductRegistrationService {
29 /**
30 * Logger used primarily for logging events that are of interest to
31 * developers.
33 private $logger;
35 /**
36 * The product registration repository to be used for db CRUD operations.
38 private $repository;
40 /**
41 * Default constructor.
43 public function __construct() {
44 $this->logger = new \common\logging\Logger("\services\ProductRegistrationService");
45 $database = \common\database\Connector::Instance();
46 $entityManager = $database->entityManager;
47 $this->repository = $entityManager->getRepository('\entities\ProductRegistration');
50 public function getProductStatus() {
51 $this->logger->debug('Getting current product registration status');
52 $row = $this->repository->findFirst();
54 // Unboxing these here to avoid PHP 5.4 "Can't use method
55 // return value in write context" error.
56 $id = '';
57 $optOut = '';
59 if ($row !== null) {
60 $id = $row->getRegistrationId();
61 $optOut = $row->getOptOut();
64 if (empty($row)) {
65 $row = new \entities\ProductRegistration();
66 $row->setStatusAsString('UNREGISTERED');
67 } else if ($id !== 'null') {
68 $row->setStatusAsString('REGISTERED');
69 } else if (!empty($optOut) && $optOut == true) {
70 $row->setStatusAsString('OPT_OUT');
73 return $row;
76 public function registerProduct($email) {
77 if (!$email || $email == 'false') {
78 $this->optOutStrategy();
79 return null;
80 } else {
81 return $this->optInStrategy($email);
85 private function optInStrategy($email) {
86 $this->logger->debug('Attempting to register product with email ' . $email);
87 $curl = curl_init('https://reg.open-emr.org/api/registration');
88 curl_setopt($curl, CURLOPT_POST, true);
89 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('email' => $email)));
90 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
91 $responseBodyRaw = curl_exec($curl);
92 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
93 curl_close($curl);
95 $this->logger->debug('Raw response from remote server: ' . $responseBodyRaw);
96 switch ($responseCode) {
97 case 201:
98 $responseBodyParsed = json_decode($responseBodyRaw);
100 $entry = new \entities\ProductRegistration();
101 $entry->setRegistrationId($responseBodyParsed->productId);
102 $entry->setEmail($email);
103 $entry->setOptOut(false);
105 $newId = $this->repository->save($entry);
106 $this->logger->debug('Successfully registered product ' . $newId);
108 return $newId;
109 break;
110 case 400:
111 throw new \InvalidEmailException($email . ' ' . xl("is not a valid email address"));
112 break;
113 case 409:
114 throw new \DuplicateRegistrationException(xl("Already registered"));
115 break;
116 default:
117 throw new \GenericProductRegistrationException(xl("Server error: try again later"));
121 // void... don't bother checking for success/failure.
122 private function optOutStrategy() {
123 $this->logger->debug('Attempting to opt out of product registration');
124 $entry = new \entities\ProductRegistration();
125 $entry->setRegistrationId('null');
126 $entry->setEmail(null);
127 $entry->setOptOut(true);
129 $this->repository->save($entry);